一般情况下我们通过<script>标签来引用JS脚本
<script src="123.js"></script>
但是在某些情况下,JS脚本会带有ES6的import关键字用来导入其他模块,比如:
//123.js
import ABC from "./ABC.js";
ABC("Hello World");
//ABC.js
export default function (String) {
console.log(String);
}
↑ 示例脚本如上 ↑
Uncaught SyntaxError: Unexpected identifier
浏览器出现错误:提示不识别该脚本中的import标识符
<script type="module" src="ABC.js"></script>
修复浏览器错误:显式指定脚本类型为“Module”
Access to script at 'file:///C:/123.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
GET file:///C:/123.js net::ERR_FAILED
浏览器依然报错,提示被CORS策略阻止,不能直接通过文件路径引用带有import关键字的JS脚本
不使用服务器,不编译这些脚本文件,不使用第三方工具
只在纯Chrome浏览器上进行调试,那么该如何解决跨域这个问题?
搬运一个 stackoverflow 答案:
大部分浏览器不支持本地文件的 CORS header 。所以你要么起一个服务,要么关掉 CORS。