`

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

 
阅读更多

在学习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。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics