【发布时间】:2015-07-23 17:43:44
【问题描述】:
我正在尝试使用此脚本加载 XML 文件,但是当我运行代码并按下“按钮”时,我收到以下消息:
阻止来自其他来源的请求:同源策略阻止 远程资源读取https://therouteofmyxml.xml(原因: 缺少 CORS ' Allow- Access- Control- header Origin ')。
是因为我的浏览器上有任何扩展程序或插件吗?谢谢!!
<script>
function loadXMLDoc(){
xmlhttp=null;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null){
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
xmlDoc=xmlhttp.responseXML;
var txt="";
x=xmlDoc.getElementsByTagName("offer");
for (i=0;i<x.length;i++){
txt=txt + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("myDiv").innerHTML=txt;
}
}
xmlhttp.open("GET","https://therouteofmy.xml",true);
xmlhttp.send();
}
else{
alert("Your browser does not support XMLHTTP.");
}
}
</script>
</head>
<body>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Mis Ofertas</button>
【问题讨论】:
-
不,它与扩展或插件无关。出于安全原因,您通常不应该从另一个域加载 XML。对同源政策进行一些研究以获取更多信息。