XML请求-getElementsByTagName没有函数

XML Request - getElementsByTagName nog a function?

本文关键字:函数 -getElementsByTagName 请求 XML      更新时间:2023-09-26

我目前正在使用此代码读取XML文件:

<script type="text/javascript">function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (true) {
    myFunction(xhttp);
}
};
xhttp.open("GET", "../wp-content/uploads/2015/12/zuiderspel.xml", true);
xhttp.send();
}
function myFunction(xml){
console.log(xml.response);
var x, i, xmlDoc, table;
xmlDoc = xml.response;
x = xmlDoc.getElementsByTagName("name")[0];
    console.log(x);
}
loadDoc();
</script>

(顺便说一句,我必须在Stackoverflow中的每行前面放4个空格才能将其放入代码块中,这很乏味,还会破坏凹痕。我该如何更容易做到这一点?)

我的问题:我明白错误(索引):168未捕获类型错误:xmlDoc.getElementsByTagName不是函数

为什么会这样?使用上的示例http://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest_xml这应该是对的,但事实并非如此。。

谢谢!

当您说xmlDoc = xml.response时,示例显示xmlDoc = xml.responseXML