Chrome 62/Chrome Canary 64中的ES6模块支持在本地不起作用,CORS错误 [英] ES6 module support in Chrome 62/Chrome Canary 64, does not work locally, CORS error

查看:304
本文介绍了Chrome 62/Chrome Canary 64中的ES6模块支持在本地不起作用,CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Index.html

Index.html

<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';
}

错误:CORS策略阻止了对源'null'处'file:///C:*******/es6/code.js'处脚本的访问:无效的响应.因此,不允许访问原始空".

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.

Chrome表示它可以支持模块,并且我在网上看到了一些示例,但是当我复制下载的示例并在本地运行它们时,总是会遇到上述错误.我不想使用Babel,Webpack等.

Chrome says it can support modules and I have seen examples working on the web, but when I copy them of download and run them locally, I always get the error above. I do not want to use Babel, Webpack, etc.

我尝试过在Chrome和Chrome Canary中启用实验性Web平台功能"标志.

I have tried enabling the Experimental Web Platform features flag in both Chrome and Chrome Canary.

推荐答案

与常规脚本不同,ES6模块受

Unlike regular scripts, ES6 modules are subject to same-origin policy. This means that you cannot import them from the file system or cross-origin without a CORS header (which cannot be set for local files).

基本上,您需要从(本地)服务器上运行此代码,或者在浏览器中禁用相同来源的代码以进行测试(请不要永久执行此操作).请参阅:从来源访问图像' null"已被CORS政策阻止.

Basically you need to run this code from a (local) server or disable same-origin in the browser for testing (do not do this permanently). See: Access to Image from origin 'null' has been blocked by CORS policy.

这篇关于Chrome 62/Chrome Canary 64中的ES6模块支持在本地不起作用,CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆