关于chrome调试AJAX出现"origin-null-is-not-allowed-access"

本文探讨了使用AJAX加载本地HTML文件时遇到的跨域问题,特别是针对谷歌浏览器的限制进行了说明,并给出了使用不同浏览器及搭建本地web服务器进行测试的解决方案。

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

在学习AJAX,用jQuery调用load()方法加载另外html文件时,出现了错误Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.XMLHttpRequest cannot load file:///E:/code/JQuery/AJAX/a.html.

 

然后google了一下,在stackoverflow上看到以下解释:

Origin nullis the local file system, so that suggests that you're loading the HTML page that does the load call via a file:/// URL (e.g., just double-clicking it in a local file browser or similar). Different browsers take different approaches to applying the Same Origin Policy to local files. My guess is that you're seeing this using Chrome. Chrome's rules for applying the SOP to local files are very tight, it disallows even loading files from the same directory as the document. So does Opera. Some other browsers, such as Firefox, allow limited accessto local files. But basically, using ajax with local resources isn't going to work cross-browser. If you're just testing something locally that you'll really be deploying to the web, rather than use local files, install a simple web server and test via http:// URLs instead. That gives you a much more accurate security picture.

 

 

大致意思就是因为load本地的HTML文件,谷歌浏览器对于这种行为比较苛刻,禁止访问本地文件。 但是火狐允许,所以用火狐测试通过。这里回答者建议搭建一个简单的web server来测试AJAX。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值