我试图理解javascript中的模块,但是当我尝试编写一个非常简单的模块时,我得到CORS错误。我使用括号作为我的文本编辑器,而奇怪的部分是当我在括号中使用实时预览时代码可以工作但是当我正常打开.js文件时,我收到错误。
I am trying to understand modules in javascript but when I try to write a very simple module I get CORS error.I am using bracket as my text editor and the odd part is when Im using live preview in brackets the code works but when I normally open the .js file I get error.
index.html
index.html
<!DOCTYPE html> <html> <head> <title> JS </title> </head> <body> <script type="module"> import {add} from './libA.js'; console.log(add(10,20)); </script> </body> </html>libA.js
export function add(a,b) { return a+b ; }*我收到此错误 - >在'file://访问脚本来自'null'的/F:/WEB%20DEV/JavaScript/libA.js'已被CORS策略阻止:响应无效。因此不允许原点'null'访问。 (我也尝试最新版本的chrome)
*I get this error -> Access to Script at 'file:///F:/WEB%20DEV/JavaScript/libA.js' from origin 'null' has been blocked by CORS policy: Invalid response. Origin 'null' is therefore not allowed access. (I try the latest version of chrome too)
推荐答案许多浏览器都不允许你访问使用JavaScript的本地文件系统(即使HTML文档也在本地文件系统上)。
Many browsers do not allow you to access files on the local filesystem with JavaScript (even if the HTML document is also on the local filesystem).
安装网络服务器并使用它(使用类似的URL)用于测试的http:// localhost / 。
Install a webserver and use that (with a URL like localhost/) for testing.
更多推荐
javascript模块和CORS
发布评论