CORS Error: Cross origin requests are only supported for HTTP; XMLHttpRequest; 加载本地文件html

本文介绍了解决在本地环境中加载文件时遇到的CORS错误的方法,通过使用NodeJS和http-server或Python的http.server模块,创建一个本地服务器,从而绕过浏览器的安全限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在打开本地文件时出现CORS Error: Cross origin requests are only supported for HTTP,无法加载本地文件。

 

出错原因:

解决方法:

1.Install NodeJS // Hint: If you're on a Mac, you want to install and use Homebrew for this

(安装NodeJS //提示:如果你在用Mac,可以下载Homebrew来安装这个)

2.Open your favorite terminal emulator

(打开一个你喜欢的终端 cmd, git bash之类的)

3.Install http-server by running: npm install http-server -g

(通过运行 npm install http-server -g安装http-server)

4.Start http-server by running: http-server 'path/to/your/angular/root/directory' -o

通过运行 http-server 'path/to/your/angular/root/directory' -o 来运行http-server

That last command — specifically, the -o flag — should open a browser window at: localhost:8080

最后一条命令,特别是其中的-o标志,应该在地址localhost:8080打开了一个浏览器窗口

Your angular app should work now; just type in localhost:8080 in your browser's address bar. Remember that from now on — assuming you've completed steps 1-3 above — anytime you desire a server instance, all you have to do is step 4.

或者你已经安装了python,那么打开cmd,进入存放.html文件的文件夹, 输入

python -m http.server

然后打开一个浏览器,在地址栏输入localhost:8000

就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值