我无法看到我在哪里犯了错误:我的桌子是空的,我想使用控制台进行调试但不知道如何操作。
更新:此示例有效http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2我使用了代码,但使用不同的xml我不明白为什么我的工作不正常
<!DOCTYPE html>
<html>
<head>
<title>XML Data Block Demo</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse:collapse;
}
th, td {
padding: 5px;
}
</style>
<script>
function parseXML(input) {
var xml = input.responseXML;
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
var lineItems = doc.getElementsByTagName("Stock");
var table="<tr><th>Ticker</th><th>Price</th></tr>";
for (i = 0; i <lineItems.length; i++) {
table += "<tr><td>" +
lineItems[i].getElementsByTagName("Ticker")[0].childNodes[0].nodeValue +
"</td><td>" +
lineItems[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("table").innerHTML = table;
}
function loadXML() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
parseXML(xhttp);
}
};
xhttp.open("GET", "http://localhost/ajax/xml/demo1/stocks.xml", true);
xhttp.send();
}
</script>
</head>
<body onload="loadXML()";>
<table id="table"></table>
</body>
</html>
答案 0 :(得分:2)
我使用stocks.xml的内容测试了你的函数,它在ie11上工作正常,所以我建议你进行以下更改:
function parseXML(input) {
var xml = input.responseXML || input.responseText;
var doc;
try {
var parser = new DOMParser();
doc = parser.parseFromString(xml, "application/xml");
} catch(err) {
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = false;
doc.loadXML(xml);
}
完整的代码是:
<!DOCTYPE html>
<html>
<head>
<title>XML Data Block Demo</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse:collapse;
}
th, td {
padding: 5px;
}
</style>
<script>
function parseXML(input) {
var xml = input.responseXML || input.responseText;
var doc;
try {
var parser = new DOMParser();
doc = parser.parseFromString(xml, "application/xml");
} catch(err) {
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = false;
doc.loadXML(xml);
}
var lineItems = doc.getElementsByTagName("Stock");
var table="<tr><th>Ticker</th><th>Price</th></tr>";
for (i = 0; i <lineItems.length; i++) {
table += "<tr><td>" +
lineItems[i].getElementsByTagName("Ticker")[0].childNodes[0].nodeValue +
"</td><td>" +
lineItems[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("table").innerHTML = table;
}
function loadXML() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
parseXML(xhttp);
}
};
xhttp.open("GET", "http://localhost/ajax/xml/demo1/stocks.xml", true);
xhttp.send();
}
</script>
</head>
<body onload="loadXML()";>
<table id="table"></table>
</body>
</html>
xml内容为:
<?xml version="1.0"?>
<portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes">
<stock exchange="nasdaq">
<name>new</name>
<symbol>zzzz</symbol>
<price dt:dt="number">20.313</price>
</stock>
<stock exchange="nyse">
<name>zacx corp</name>
<symbol>ZCXM</symbol>
<price dt:dt="number">28.875</price>
</stock>
<stock exchange="nasdaq">
<name>zaffymat inc</name>
<symbol>ZFFX</symbol>
<price dt:dt="number">92.250</price>
</stock>
<stock exchange="nasdaq">
<name>zysmergy inc</name>
<symbol>ZYSZ</symbol>
<price dt:dt="number">20.313</price>
</stock>
</portfolio>
我的计算机上使用Chrome / IE / FireFox最后版本的结果是: