javascript模块和CORS

编程入门 行业动态 更新时间:2024-10-11 03:16:08
本文介绍了javascript模块和CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图理解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

本文发布于:2023-10-10 17:46:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   javascript   CORS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!