Home > Enterprise >  Getting "Cross-origin-request Blocked" on axios post request
Getting "Cross-origin-request Blocked" on axios post request

Time:09-22

public addArtikel(): void {
        this.neuArtikel.push(this.ArtikelObj);
        // console.log(toJS(this.neuArtikel));

         const config = {
            headers: { "Access-Control-Allow-Origin": "true" },
         };

        axios.defaults.headers.post["Access-Control-Allow-Origin"] = "*";

        axios
            .post("http://172.16.101.250:3000/create", this.ArtikelObj ,config)
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.log(error);
            });
    }

please i get a Cors Error when i try to make an axios post request to my Gateway service i try to use two methods to enable my cros origin but no way :

CodePudding user response:

from my experience i think the CORS needs to be handled from your back-end (API) check out the CORS configuration you have in your back-end

CodePudding user response:

There is nothing to do with post api request instead the server needs to respond with CORS headers on the options call. If you are using nodejs/express as backend, you can fix this for all endpoints with:

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content- 
  Type, Accept');
  next();
});
  • Related