【问题标题】:Getting CORS blocked even after ("Access-Control-Allow-Origin", "*")即使在(“Access-Control-Allow-Origin”,“*”)之后也阻止了 CORS
【发布时间】:2019-09-02 14:32:05
【问题描述】:

无论我允许什么,我都会阻止 CORS。它只会对我的 POST 路线大喊大叫,它允许的 GET 路线很好。我一直得到

从源“http://simple-startup-survey.surge.sh”访问“https://simple-startup-survey-backend.herokuapp.com/client_answers”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“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) {

地球上每一个该死的谷歌搜索结果都表明我的后端应该有效。我使用的是香草 JS,没有 JQUERY。 非常感谢任何帮助!

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

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

【问题讨论】:

    标签: ajax express


    【解决方案1】:

    我遇到了类似的问题。

    我的预感是,surge.sh 在其服务器上没有启用 cors。

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

    编辑:已解决 - - - -

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

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

    这是一篇关于为浪涌.sh https://github.com/surge-sh/example-cors 启用 cors 的文章

    此外,在进行更改时,请确保您每次都在一个新的隐身标签中,或者转到开发工具上的网络标签并禁用缓存,否则您的应用可能会对您撒谎,并且实际上启用了事情 cors 并没有启用.

    【讨论】:

      猜你喜欢
      • 2020-03-31
      • 2020-01-18
      • 2021-06-29
      • 2019-04-26
      • 2019-10-02
      • 2019-09-20
      • 2021-03-21
      • 2018-04-19
      • 2020-04-07
      相关资源
      最近更新 更多