ES6模块支撑在Chrome 62 /铬金丝雀64,不会局部工作,CORS错误
问题描述:
的index.htmlES6模块支撑在Chrome 62 /铬金丝雀64,不会局部工作,CORS错误
<html>
<head>
<script type="module">
import {answer} from './code.js'
console.info("It's ${answer()} time!")
</script>
</head>
<body>
</body>
</html>
code.js
export function answer(){
return 'module';
}
Error: Access to Script at 'file:///C:*******/es6/code.js' from origin 'null' has been blocked by CORS policy: Invalid response. Origin 'null' is therefore not allowed access.
铬说,它可以支持模块我看到了一些在网络上工作的例子,但是当我将它们复制下载并在本地运行时,我总是得到上面的错误。我不想使用Babel,Webpack等。
我曾尝试在Chrome和Chrome Canary中启用实验性Web平台功能标志。
答
与常规脚本不同,ES6模块受same-origin policy的限制。这意味着您不能从文件系统或跨源的import
这些文件系统没有CORS头(不能为本地文件设置)。
基本上,您需要从(本地)服务器运行此代码或在浏览器中禁用同一来源进行测试(不要永久执行此操作)。请参阅:Access to Image from origin 'null' has been blocked by CORS policy。
谢谢亚历山大! 任何想法如果我尝试使用电子会发生什么? –
@markpavlis我猜想Electron没有这样的安全限制,但我不确定。 –