No 'Access-Control-Allow-Origin in reactJS

好久不见. 提交于 2020-03-19 05:10:46

问题


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': '*',
  }
};

回答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



来源:https://stackoverflow.com/questions/51577526/no-access-control-allow-origin-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!