我有一个生成的 React 站点,我托管在 S3 存储桶中。我的一个组件在加载时尝试获取一些东西:
require('isomorphic-fetch')
...
componentDidMount() {
fetch(`${url}`)
.then(res => {
console.log(res);
this.setState({
users: res
})
})
.catch(e => {
// do nothing
})
}
Run Code Online (Sandbox Code Playgroud)
在url我取是AWS API网关。我通过下拉菜单在那里启用了 CORS,对默认配置没有任何更改。
在我的控制台中,对于远程站点和开发过程中的本地站点,我看到:
“加载失败url:请求的资源上不存在‘Access-Control-Allow-Origin’标头。” 等等
但是,在 Chrome 网络选项卡中,我可以看到请求和响应,状态为 200 等。但是,在控制台中, myconsole.log和this.setState永远不会被调用。
我知道 CORS 是一个常见的痛点,很多问题都涉及到 CORS。我的问题:为什么响应在“网络”选项卡中没有显示错误,而同时在控制台中出现错误?
我遇到过一种情况,我从 Angular 6 应用程序调用 Spring Boot 应用程序。当我以与在不同端口上运行的应用程序的角度调用 HTTP post 方法时,它会引发异常。
从源“ http://localhost: 4200 ”访问位于“http://localhost:8080/api”的XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP好的状态。
我的 Angular 应用程序在“ http://localhost:4200 ”上运行,端口号:4200 我的 Spring Boot 应用程序在“ http://localhost:8080/api ”上运行:端口号:8080。
没有直接的答案可以解决这个问题。有一些技巧可以禁用 Chrome 中的安全性,使用 NGINX 可以解决此问题。
我尝试使用 ajax 从我的侧客户端连接到 API 服务器,但在响应中出现错误: 访问 XMLHttpRequest at ' https://localhost:44381/api/webinars ' from origin ' http://localhost :5003 ' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 我尝试在 API 中编辑 CROS 策略,但仍然出现相同的错误。这是我来自 MVC 的 ajax 代码:
$.ajax({
url: "https://localhost:44381/api/webinars",
type: 'POST',
headers: {
contentType: "application/json",
},
body: JSON.stringify(body),
success: function (data) {
console.log(data);
},
failure: function (err) {
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
从 API 启动:
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. …Run Code Online (Sandbox Code Playgroud) 我是烧瓶和角度的新手,请耐心等待。
\n\n我一直被 CORS 的问题困扰。我应用了不同的代码修复只是为了使其工作。现在我得到的错误是
\n\nAccess to XMLHttpRequest at \'http://localhost:5000/dashboard/clientscount/2019/2020\' from origin \'http://localhost:8080\' has been blocked by CORS policy: Response to preflight request doesn\'t pass access control check: It does not have HTTP ok status.\nRun Code Online (Sandbox Code Playgroud)\n\n我认为我的问题的答案来自这篇文章的已回答问题:已被 CORS 策略阻止:对预检请求的响应不\xe2\x80\x99t 通过访问控制检查,特别是此代码
\n\nif r.Method == "OPTIONS" {\n w.WriteHeader(http.StatusOK)\n return\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但这是用go语言写的。我正在烧瓶中工作。我的问题是如何在烧瓶中制作这个?同样在参考答案中,它说响应最初的请求,但我不确定如何继续该请求。
\n\n如果您能为我指出正确的方向或文档,我将非常感激。
\nangular ×2
cors ×2
ajax ×1
angular6 ×1
api ×1
c# ×1
cross-domain ×1
fetch-api ×1
flask ×1
flask-cors ×1
javascript ×1
spring-boot ×1