1.问题
使用ajax请求本地数据时,在浏览器调试代码发现控制台报错:
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
2.原因
以上错误提示是由于ajax方法涉及到跨域的问题导致,由于没有在服务器环境里运行含有ajax方法的页面,而是直接通过浏览器打开(类似file:///的访问形式,即file协议)
本地页面ajax()请求本地页面,须通过服务器环境运行,类似这样:
http://127.0.0.1:8888/index.html
3.解决方法
给浏览器传入启动参数(allow-file-access-from-files),允许浏览器进行跨域访问。
找到Chrome浏览器的快捷方式,右键访问“属性”,找到“目标”,然后在后面加上 --allow-file-access-from-files,注意前面要有空格,不然会报错。填好之后选择右下角的“应用”,然后重启服务器。
填好如下:
C:\Users\yang\AppData\Local\Google\Chrome\Application\chrome.exe –-allow-file-access-from-files
参考文章:
https://www.cnblogs.com/micua/p/chrome-file-protocol-support-ajax.html
https://blog.csdn.net/DFF1993/article/details/80975878