Javascript 加载本地css和js文件时出现Cors错误
使用脚本和链接标记加载js/css文件(来自vue项目)时,浏览器拒绝将其加载。 以下是错误:Javascript 加载本地css和js文件时出现Cors错误,javascript,html,css,cors,content-security-policy,Javascript,Html,Css,Cors,Content Security Policy,使用脚本和链接标记加载js/css文件(来自vue项目)时,浏览器拒绝将其加载。 以下是错误: 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 res
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上
由于标签是由warn build
生成的,它使用vue cli生成生产版本,因此我无法控制标签的生成方式
下面是带有内容安全策略元标记的index.html
<!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>
申请名称
很抱歉,如果未启用JavaScript,Vue.js将无法正常工作。
请使其继续。
如果有人对如何解决这个问题有什么建议,请告诉我
www.example.com
和example.com
不是同一来源
要避免此问题,请选择一个规范化的,并将所有其他请求重定向到它。我将所有内容重定向到
www.example.com
,这在重新加载apache服务器后为我修复了此问题。非常感谢。