Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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': '*',
  }
};
like image 866
Moein Hosseini Avatar asked Nov 07 '22 04:11

Moein Hosseini


1 Answers

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.

like image 109
cdoshi Avatar answered Nov 15 '22 05:11

cdoshi