Moein Hosseini
Moein Hosseini

Reputation: 730

No 'Access-Control-Allow-Origin in reactJS

I want to know how set Access-Control-Allow-Origin in axios post method at reactJS or react-native environment? I use CORS Add-ons and it works but I want to set it in the header too, I try these ways but none of them does not work.

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

and

let axiosConfig = {
  headers: {
    'method':'POST',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Access-Control-Allow-Origin': '*',
  }
};

Upvotes: 5

Views: 6413

Answers (1)

cdoshi
cdoshi

Reputation: 2832

You need to enable cross origin request at your server end. A snippet of how you can do it using Express would be as follows

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();
});

This would enable all requests to support CORS. Adapt it as per your requirements.

Check this link for more information if you can not change the server.

Upvotes: 1

Related Questions