5

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': '*',
  }
};
Moein Hosseini
  • 662
  • 1
  • 11
  • 28

1 Answers1

1

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.

cdoshi
  • 2,772
  • 1
  • 13
  • 20