一些跨域问题

问题1:console面板出现Access to Font at 'http://static.anhouse.com/iconfont_c4f1eed.wolf' from origin 'http://static.anhouse.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xf.pinganfang.com' is therefore not allowed access.报错。

原因:Access-Control-Allow-Origin是HTML5中定义的一种服务器端返回Response header,用来解决资源(比如字体)的跨域权限问题。

解决方案:需要后端或运维在nginx上添加一个同域的配置,把static这个域添加到同域。
在nginx.conf中配置

http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  ......
}

问题2:打开本地html文件时,console面板出现

Cross origin requests are only supported for protocol schemes: http, data,chrome-extension, https, chrome-extension-resource.报错。

原因:做本地文件上传,浏览器为了安全性考虑,默认对跨域访问禁止。

解决方案:给浏览器传入启动参数(allow-file-access-from-files),允许跨域访问。

Windows下,运行"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --allow-file-access-from-files

原文地址:https://www.cnblogs.com/camille666/p/cross_domain_cases.html