我得到了错误“;TypeError:xml.getElementsByTagName不是函数;

Im getting the error "TypeError: xml.getElementsByTagName is not a function"

本文关键字:getElementsByTagName xml 函数 TypeError 错误      更新时间:2023-09-26

我收到错误"TypeError:xml.getElementsByTagName不是函数"

错误在于"var xmlDoc=new DOMParser().parseFromString(xml,'text/xml');"

我该怎么解决这个问题?我已经为此工作了几个小时,但仍然没有结果

<!DOCTYPE html>
<html>
 <meta charset="UTF-8"> 
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (xhttp.readyState == 4 && xhttp.status == 200) {
       myFunction(xhttp);
    }
}
xhttp.open("GET", "http://LEMONPIE-PC/erdas-iws/ogc/wms/?service=WMS&request=getcapabilities", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = new DOMParser().parseFromString(xml,'text/xml');
    console.log(xmlDoc);
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("Layer");
    for (i=0;i<x.length;i++)
    { 
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("Layer")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Style")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");

}

</script>
</body>
</html>
myFunction(xhttp);  <-- Look at what you are passing to the method

您正在传递XMLHttpRequest对象,并将其视为文本,但事实并非如此。您需要引用XMLHttpRequest对象所包含的responseText。

myFunction(xhttp.responseText);

或者,如果您正在获取XML,我不知道为什么要再次解析它,因为XMLHttpRequest对象将为您完成这项工作。只要它是有效的XML文档,它就应该与responseXML一起使用。

myFunction(xhttp.responseXML);  
相关文章:
  • 没有找到相关文章