【发布时间】: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
【问题讨论】: