本文介绍了XMLHttpRequest 无法加载跨源请求仅支持 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在从 XML 文件获取数据时发现异常 101:XMLHttpRequest 无法加载 file:///C:/Users/zaid/Desktop/xml/cd_catalog.xml.跨源请求仅支持 HTTP.
found an exception 101 in getting data from XML file:XMLHttpRequest cannot load file:///C:/Users/zaid/Desktop/xml/cd_catalog.xml. Cross origin requests are only supported for HTTP.
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
推荐答案
您正在尝试通过本地计算机请求资源,这是一个交叉引用.您需要通过 HTTP 服务器访问此资源才能访问它.
You're trying to request a resource through your local machine, that's a cross reference. You need to access to this resource trough your HTTP server to get access to it.
这篇关于XMLHttpRequest 无法加载跨源请求仅支持 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!