账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    直接引用带有import关键字的JS脚本会报错
    73
    0

    一般情况下我们通过<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浏览器上进行调试,那么该如何解决跨域这个问题?

    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部