文章内容

2019/3/13 17:14:05,作 者: 黄兵

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

最近在开发Angular的时候出现了如下错误:

Access to XMLHttpRequest at 'https://localhost:5001/api/values' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.


出现这个问题的原因:

出现了跨域请求。


解决方案:

如果是在开发环境中,在Windows中,将此命令粘贴到运行窗口中:

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

这将打开一个新的Chrome浏览器,允许访问没有'access-control-allow-origin'标头请求。

这不是生产修复或者必须向客户端显示应用程序时,这仅在UI和后端开发 位于不同服务器上并且在生产中它们实际上位于同一服务器上时才有用例如:在为任何应用程序开发UI时,如果需要在本地测试它指向后端服务器,那么这是一个完美的修复。对于生产修复,必须将CORS头添加到后端服务器以允许跨源访问。


参考资料:Why does my JavaScript get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when Postman does not?

分享到:

发表评论

评论列表