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.
在尝试以chrome.exe --allow-file-access-from-files的方法运行chorm和移动文件位置等方法仍然不能解决报错问题后,找到了解决的方法:
1.在Firefox上运行,不用 goole chorm
2.在本地架设一个服务器,在服务器上运行
|
|
以下是chrome加后缀方法 的转载:————————————————————————————————————————————
配置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的问题,参考: