加载本地CSS和JS文件时出现Cors错误

时间:2018-11-30 10:47:23

标签: javascript html css cors content-security-policy

使用脚本和链接标签从ue项目中加载js / css文件时,浏览器拒绝将其加载。 这些是错误:

Access to script at 'http://www.example.com/js/app.7f934f5b.js' from origin 'http://example.com' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

此错误在iOS和macOS上的最新Safari以及Chrome的最新开发人员版本中均发生。

由于标签是由yarn build生成的,<!DOCTYPE html> <html lang="en"> <head> <base href="<%= BASE_URL %>"> <meta http-equiv="Content-Security-Policy" content="default-src self; script-src self 'unsafe-inline' 'unsafe-eval' http: https:; style-src self 'unsafe-inline' http: https:; img-src self data: http: https:; font-src self http: https:; connect-src http: https: ws:"> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width,initial-scale=1.0"/> <link rel="icon" href="<%= BASE_URL %>favicon.ico"/> <link href="fontawesome/css/all.css" rel="stylesheet"/> <title>Application title</title> </head> <body> <noscript> <strong> We're sorry but Vue.js doesn't work properly without JavaScript enabled. Please enable it to continue. </strong> </noscript> <div id="app"> </div> </body> </html> 使用vue cli来构建生产版本,因此我无法控制标签的生成方式。

下面是带有content-security-policy元标记的index.html。

CDate()

如果有人对如何解决此问题有任何建议,请告诉我!

1 个答案:

答案 0 :(得分:1)

www.example.comexample.com不同原点

为避免此问题,请选择一个规范的请求,然后将所有其他请求重定向到该请求。