它适用于 Firefox,但不适用于 Chrome 和 IE。
我在本地试试。我在 httpObj.send(null) 上收到错误;线。
我该如何处理这个问题?
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>
<title>XML READ</title>
<script type="text/javascript">
//---
function GetXml() {
if (window.XMLHttpRequest) {
var httpObj = new XMLHttpRequest();
} else {
var httpObj = new ActiveXObject("Microsoft.XMLHTTP");
}
httpObj.open("GET", "notification.xml", false);
// Error Starts Here
httpObj.send( null );
var xmlDocument = httpObj.responseXML;
var xmlEl = xmlDocument.getElementsByTagName("haber");
//--
for (i = 0; i < xmlEl.length; i++) {
for (j = 0; j < xmlEl[i].childNodes.length; j++) {
if (xmlEl[i].childNodes[j].nodeType != 1) {
continue;
}
alert(xmlEl[i].childNodes[j].firstChild.nodeValue);
}
}
}
</script>
</head>
<body onload="GetXml()">
</body>
</html>
XML 文件
<?xml version="1.0" encoding="utf-8" ?>
<notifications>
<notification id="001">
<name>First</name>
</notification>
<notification id="002">
<name>Second</name>
</notification>
</notifications>