我有一个xml文件和一个引用它的文件。在Firefox中,我得到了我期待的响应,但在我安装的其他两个浏览器中却没有(Chrome& IE9)。 谁能告诉我,我做错了什么? HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test File List Document</title>
</head>
<body>
<div>Output</div>
<div id="testDiv"></div>
</body>
<script>
//XML request
var xmlhttp, xmlDoc;
// code for IE6, IE5
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
};
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "shredder-data.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
//test
document.getElementById("testDiv").innerHTML=xmlDoc.getElementsByTagName("crc")[0].childNodes[0].nodeValue;
</script>
</html>
XML(shredder-data.html}
<?xml version="1.0" encoding="utf-8"?>
<shredders>
<personal>
<model name="P-20">
<headline>Fellowes PowerShred P-20 Strip-Cut Personal Shredder</headline>
<users>1</users>
<crc>3401401</crc>
<cut>Strip</cut>
<jam>no</jam>
<sheet_capacity>5</sheet_capacity>
<run_time>2/25</run_time>
<bin_capacity>11</bin_capacity>
<staples>no</staples>
<paperclips>no</paperclips>
<credit-cards>no</credit-cards>
<CDs>no</CDs>
</model>
<model name="P-35c">
<headline>Fellowes Powershred P-35C Cross-Cut Personal Shredder with Safety Lock</headline>
<users>1</users>
<crc>3008801</crc>
<cut>Strip</cut>
<jam>no</jam>
<sheet_capacity>2-4/15</sheet_capacity>
<run_time>2/25</run_time>
<bin_capacity>4.5</bin_capacity>
<staples>yes</staples>
<paperclips>no</paperclips>
<credit-cards>yes</credit-cards>
<CDs>no</CDs>
</model>
</personal>
</shredders>
我希望的输出是:
Output
3401401
答案 0 :(得分:0)
您在脚本中有一个XHR调用。默认情况下,Chrome不会通过XHR从本地文件系统加载文件。如果通过file:// protocol查看示例,则会影响资源的加载。要启用加载此类文件,您应该使用标志--allow-file-access-from-files。
IE也有一些限制,尽管有条件可以使本地访问的XHR调用起作用。
或者,从Web服务器而不是本地文件系统提供此服务。