No 'Access-Control-Allow-Origin' header is present on the requested resource.
No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
CORS是什么?
CORS (Cross-Origin Resource Sharing,跨域资源共享)是一个系统,它由一系列传输的HTTP头组成,这些HTTP头决定浏览器是否阻止前端 JavaScript 代码获取跨域请求的响应。
同源安全策略 (same-origin policy)默认阻止“跨域”获取资源。但是 CORS 给了web服务器这样的权限,即服务器可以选择,允许跨域请求访问到它们的资源。
错误信息 blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
如果请求发送方和服务提供方不在同一个源/域内,服务器返回的响应不包含Access-Control-Allow-Origin字段,或者Access-Control-Allow-Origin字段和http请求的origin不匹配,相应会被浏览器拦截,例如
…has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
解决方法
服务器端 - nginx设置CORS
在nginx对应的site配置文件中打开CORS,同时支持预检请求(preflight request)。
1 | # |
其它服务器打开CORS,请参考[这里](enable cross-origin resource sharing (enable-cors.org)).