即使在(“ Access-Control-Allow-Origin”,“ *”)之后,也会阻止CORS

时间:2019-04-07 23:07:55

标签: ajax express

无论我允许什么,我都会被CORS阻止。它只对我的POST路由大吼大叫,而我的GET路由则允许。我一直得到

CORS策略已阻止从来源“ https://simple-startup-survey-backend.herokuapp.com/client_answers”访问“ http://simple-startup-survey.surge.sh”处的XMLHttpRequest:请求的资源上没有“ Access-Control-Allow-Origin”标头。

我正在使用AJAX与EXPRESS服务器通信。我允许通配符运算符发出所有请求。我尝试将{crossDomain:true}与我的请求一起发送。

///这是我在app.js中的后端

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,POST,DELETE,PATCH,PUT");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

//这是我的前端AXIOS调用POST请求。这就是//触发CORS块

 storedData = storedData.concat(storedData2)
     axios.post('https://simple-startup-survey-backend.herokuapp.com/client_answers', {crossDomain:true}, storedData,)
     .then(function(response){
       console.log(response.data , ' save success')
       localStorage.setItem("storedData", JSON.stringify(storedData))
       window.location.href = "../AnalysisPage/analysis.html";
     }).catch()
   })

//这是我功能完善的GET请求。之后,它会在.then中执行一堆// stuff的工作,但是我认为这与// this issue无关

function getgeneralQuestions(){
    axios.get('https://simple-startup-survey-backend.herokuapp.com/questions/balanceSheet')
    .then(function (response) {

地球上每一个该死的Google搜索结果都说我对后端的了解应该起作用。我正在使用香草JS,没有JQUERY。 任何帮助,不胜感激!

后端Github:https://github.com/TuckerNemcek/SimpleSurveyBackend

前端Github:https://github.com/TuckerNemcek/SimpleStartupSurveyProject

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。

我的直觉是urge.sh的服务器上未启用cors。

我的网站实际上正在正确发出请求。 之后出现错误,并且不会显示数据。

编辑:已解决----

实际上,请自己解决这个问题! 这是我的发布脚本。

yarn build; cp build/index.html build/200.html; echo '*' > build/CORS; surge build appname.surge.sh

这是有关为浪涌启用cors.sh https://github.com/surge-sh/example-cors

的文章

此外,在进行更改时,请确保您每次都位于新的隐身标签页中,或确保进入开发工具上的“网络”标签页并禁用缓存,否则您的应用可能对您不利,并且实际上已启用该功能