ajax - 即使在(“Access-Control-Allow-Origin”,“*”)之后也阻止了 CORS
问题描述
无论我允许什么,我都会阻止 CORS。它只会对我的 POST 路线大喊大叫,它允许的 GET 路线很好。我一直得到
从源“ http://simple-startup-survey.surge.sh ”访问“ https://simple-startup-survey-backend.herokuapp.com/client_answers ”的 XMLHttpRequest已被 CORS 策略阻止:无“访问” -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
解决方案
我遇到了类似的问题。
我的预感是surge.sh 在其服务器上没有启用cors。
我的网站实际上是正确地提出请求。之后出现错误,数据不显示。
编辑:已解决 - - - -
实际上,只是为自己解决了这个问题!这是我的发布脚本。
yarn build; cp build/index.html build/200.html; echo '*' > build/CORS; surge build appname.surge.sh
这是一篇关于为surge.sh启用cors的文章https://github.com/surge-sh/example-cors
此外,在进行更改时,请确保您每次都在一个新的隐身标签中,或者转到开发工具上的网络标签并禁用缓存,否则您的应用程序可能会对您撒谎,并且实际上启用了事情 cors 并没有启用。
推荐阅读
- powerbi - Clockify API 和 Powerbi
- java - 如何在 Android Java 方法中形成更好的布尔值
- scala - Spark - scala - 如何将 DataFrame 转换为自定义对象?
- oracle - Oracle 查询结果为 JSON
- bash - 如果下一行符合特殊条件,如何输出当前行和下一行
- react-native - 如何在本机反应中自动显示当前位置
- javascript - 获取要加载到 amcharts 数据加载器中的天气预报数据
- dataframe - 如何在 Julia 中将缺失值插入我的数据框中
- python - For循环在一个输出后停止
- ios - 将数据从静态 tableViewCell 传递到另一个 viewController?