XMLHttpRequest cannot load file:///问题 Ajax本地跨域问题

当在Chrome中使用Ajax请求本地文件时,会遇到'Cross origin requests are only supported for HTTP'的错误。解决方法包括:使用Firefox,本地搭建服务器,或者修改Chrome启动参数如'--allow-file-access-from-files'。此外,此参数还能解决iframe和window.open的跨域问题。
摘要由CSDN通过智能技术生成

在尝试以chrome.exe --allow-file-access-from-files的方法运行chorm和移动文件位置等方法仍然不能解决报错问题后,找到了解决的方法:

1.在Firefox上运行,不用 goole chorm

2.在本地架设一个服务器,在服务器上运行




down vote accepted

If you are doing something like writing HTML and Javascript in a code editor on your personal computer, and testing the output in your browser, you will probably get error messages about Cross Origin Requests. Your browser will render HTML and run Javascript, jQuery, angularJs in your browser without needing a server set up. But many web browsers are programed to watch for cross site attacks, and will block requests. You don't want just anyone being able to read your hard drive from your web browser. You can create a fully functioning web page using Notepad++ that will run Javascript, and frameworks like jQuery and angularJs; and test everything just by using the Notepad++ menu item, RUN, LAUNCH IN FIREFOX. That's a nice, easy way to start creating a web page, but when you start creating anything more than layout, css and simple page navigation, you need a local server set up on your machine.

more detail: 点击打开链接


以下是chrome加后缀方法 的转载:————————————————————————————————————————————

配置chrome支持本地(file协议)ajax请求

配置chrome支持本地(file协议)ajax请求 网址:配置chrome支持本地(file协议)ajax请求

将html代码拖拽进入chrome通过file协议浏览时,发送的ajax请求本地文件,会报跨域错误。
XMLHttpRequest cannot load file:///E:/webs/extJS/ext-3.3.0/examples/csdn/combobox.txt?_dc=1414738973999.
Cross origin requests are only supported for protocol schemes: http, data,
chrome-extension, https, chrome-extension-resource.

解决办法是给chrome添加启动参数:--allow-file-access-from-files ,这样本地ajax请求就不会报跨域错误了。
(注意如果给chrome添加多个启动参数,每个启动参数“--”之前要有空格隔开,
如"C:\Program Files\Google\Chrome\Application\chrome.exe" --enable-file-cookies --allow-file-access-from-files)

如何添加chrome启动参数以便支持file协议下的ajax请求,
具体看这个:chrome无法保存本地设置cookie解决办法

添加了--allow-file-access-from-files启动参数后,
还可以解决本地file加载文件,导致iframe和父页无法相互访问,
window.open打开的页面使用opener为null的问题,参考:chrome本地测试访问iframe,parent,opener null解决办法


将html代码拖拽进入chrome通过file协议浏览时,发送的ajax请求本地文件,会报跨域错误。

ajax.js:18 XMLHttpRequest cannot load file:///E:/workspace/src/JavaScript_src/films.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

  解决办法是给chrome添加启动参数:--allow-file-access-from-files ,这样本地ajax请求就不会报跨域错误了。(注意如果给chrome添加多个启动参数,每个启动参数“--”之前要有空格隔开,如"C:\Program Files\Google\Chrome\Application\chrome.exe" --enable-file-cookies --allow-file-access-from-files

 

  如何添加chrome启动参数以便支持file协议下的ajax请求,具体看这个:chrome无法保存本地设置cookie解决办法

 

  添加了--allow-file-access-from-files启动参数后,还可以解决本地file加载文件,导致iframe和父页无法相互访问,window.open打开的页面使用opener为null的问题,参考:

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值