Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests

前言

  • 本地调试js时,遇到本错误
  • Google Chrome 84.0.4147.135 (正式版本) (64 位) (cohort: Stable Installs Only)
  • 修订版本 c42bd09b3f24da1698d71d3b4f57402137163566-refs/branch-heads/4147@{#1102}
  • 操作系统 Windows 10 OS Version 2004 (Build 19041.450)
  • JavaScript V8 8.4.371.23
    注:使用命令 chrome://version/ 查看 JavaScript 版本
  • import js 模块时遇错

错误详细信息

Access to script at 'xxx' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests_第1张图片

解决办法

在本地搭建一个server,通过url地址访问测试文件。
我整理了三个搭建方法:http-server实现server,serve实现server,编码实现server。
三个方法均使用Node.js。安装Node.js方法略。

解决后效果

Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests_第2张图片

你可能感兴趣的:(JavaScript)