读取本地xml或json等本地文件报错Failed to load file:///D:/xml/test.xml: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

问题如上图;
原因及解析:
在浏览器打开本地的html文件, 上面proxy中的url获取的就是一个本地文件, 协议是file://,如果是在服务器启动的话,则使用的是http或者https协议。
出于安全性考虑, Chrome默认禁止了这种用法,file协议和http/https协议不同,会被Chrome认为是跨域访问,所以会报被CORS(Cross-Origin Resource Sharing,跨域资源共享)的安全策略阻止。
原文链接:https://blog.csdn.net/oscar999/article/details/124114343
可以解决的方式是:
将Html文件挂在IIS上以网站方式去访问

读取本地xml文件参数,获取要得到的图片名,判断图片是否存在
var xmlObj = false;
function DownloadPng() {
//判断是否是IE浏览器
if (window.ActiveXObject) {
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlObj = new XMLHttpRequest();
}
xmlObj.open('GET', 'PngInfo.xml', true);
//使用异步方式所以要在XMLHttpRequest对象的状态改变时做相应的处理
xmlObj.onreadystatechange = function () {
//判断XMLHttpRequest的状态,如果是4 ,继续处理
if (xmlObj.readyState == 4) {
//需要判断返回状态时否是200
if (xmlObj.status == 200) {
DoXml();
}
}
}
//发送请求,因为是GET,所以send的内容是null
xmlObj.send(null);
function DoXml() {
var xmlDoc = xmlObj.responseXML;
var imageName = xmlDoc.getElementsByTagName("location")[0].childNodes[0].nodeValue;
console.log(imageName);
var location = "file/" + imageName + ".png";
xmlObj.open("Get", location , true);
xmlObj.send();
if (xmlObj.status == 404) {
console("图片不存在");
} else {
console("图片存在");
} }
访问的html 后缀待参数,读取参数作为文件名
例如 http://10.0.xx.xxx:8090/download.html?imagename=sssxx01
var ImageName = "";
urlinfo = window.location.href; //获取当前页面的 len = urlinfo.length;//获取url的长度 offset = urlinfo.indexOf("?");//设置参数字符串开始的位置
if (offset == -1) {
ImageName = "不存在";
}
else {
newsidinfo = urlinfo.substr(offset, len)//取出参数字符串 这里会获得类似“id=1”这样的字符串
newsids = newsidinfo.split("=");//对获得的参数字符串按照“=”进行分割 newsid=newsids[1];
ImageName = newsids[1];
}
读取本地xml或json等本地文件报错Failed to load file:///D:/xml/test.xml: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.的更多相关文章
- jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c
问题: 测试报告,使用本地的json.txt文件,结果文件读取失败,报错如下: XMLHttpRequest cannot load xxx. Cross origin requests are on ...
- 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension, https, chrome-extension-reso
使用ajax请求本地文件,chrome会报跨域错误. XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.C ...
- nodejs报错 XMLHttpRequest cannot load localhost:3000/test_date/. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
z在请求本地的时候 如果ajax的URL 前面没有http的话 就会报错 jq.js:2 XMLHttpRequest cannot load localhost:3000/test_date/. ...
- 【Google Chrome】 Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource问题解决
问题??打开Google Chrome浏览器报错如下: 结论 浏览器出于安全性考虑,默认对跨域访问禁止 解决方法 给浏览器添加启动参数 --allow-file-access-from-files ...
- XMLHttpRequest cannot load file:///E:/userdialog.html?_=1465888805734. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-reso
做前端写静态页面时,采用的是jq easyui框架 在ie与webkit总是加载的时候总是在loading... 而firefox却是正常加载的,总以为是代码写错了, 经过再三的检查以及百度查找,确认 ...
- 如何解决XMLHttpRequest cannot load file:~~~~~~~~~~~. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-res
原因:Chrome不支持本地Ajax请求. 解决: 右击Chrome浏览器快捷方式,选择“属性”,在“目标”中加上"--allow-file-access-from-files", ...
- Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
转自:https://www.zhihu.com/question/20948649?sort=created 我最近也遇到这个问题了,用传统的快捷方式加参数并没有用,不知道是不是和chrome版本有 ...
- 本地数据访问时出现跨域问题Cross origin requests are only supported for protocol schemes: ……
从桌面找到Chrome图标,右键属性,快捷方式,起始位置(安装路径) 注:在cmd中访问Program Files文件的方法 %ProgramFiles%=C:\Program Files %Prog ...
- Ajax本地跨域问题 Cross origin requests are only supported for HTTP
问题:打开本地html文件时,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrome ...
- 云笔记项目- 上传文件报错"java.lang.IllegalStateException: File has been moved - cannot be read again"
在做文件上传时,当写入上传的文件到文件时,会报错“java.lang.IllegalStateException: File has been moved - cannot be read again ...
随机推荐
- vue 复制功能
实现: let message = ` ${name} 电话:${item.phone} 部门:${item.department} 职务: ${item.post} 邮箱:${item.email} ...
- ubuntu下升级gcc11环境
使用ppa源升级 官网地址: https://launchpad.net/ ppa toolchan/test地址: https://launchpad.net/~ubuntu-toolchain-r ...
- Software_Programming_bootstrap_book
2019-10-25 HTML index 11 p24 主页布局.
- vim 小记录
将str1批量替换成str2 , 特殊符号前用转译符 \ :%s/str1/str2/g
- 073_SFDC Limit
我们在开发的过程中,应多注意一些系统自身的限制,以及遇到此类问题的应对措施: Description Synchronous Limit Asynchronous Limit Total number ...
- 电商项目maven框架搭建引入dubbo配置文件报错
解决:dubbo配置文件报红叉的问题 构建dobbo-provider配置文件时,报红叉错误,本质即找不到对应的dubbo.xsd文件. 1.下载模板 模板下载地址:http://download.c ...
- 数据库链接失败错误ERROR com.alibaba.druid.pool.DruidDataSource - {dataSource-1} init error解决
用java -jar启动之后,只要一访问数据库就报错,错误信息如下: 文字如下: 2019-07-02 10:25:12.220 ---> [http-nio-8080-exec-1] ---& ...
- 3、k8s 核心实战
7 kubernets核心实战 7.1 资源创建方式 命令行 yaml 7.2 namespace 名称空间来隔离资源 命令行方式 kubectl create ns hello kubectl de ...
- git+jenkins+ansible+gitlab部署网站
- Python学习笔记(五)if分支语句
一.if语法 示例: 1 money = int(input('请输入余额:')) 2 if money >= 5: 3 print('买得起!') 4 5 if True: 6 print(' ...