【问题标题】:Im getting the error "TypeError: xml.getElementsByTagName is not a function"我收到错误“TypeError:xml.getElementsByTagName 不是函数”
【发布时间】:2015-11-11 02:22:01
【问题描述】:

我收到错误“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>

【问题讨论】:

    标签: javascript xml


    【解决方案1】:
    myFunction(xhttp);  <-- Look at what you are passing to the method
    

    您正在传递 XMLHttpRequest 对象并将其视为文本,但情况并非如此。您需要引用 XMLHttpRequest 对象持有的 responseText。

    myFunction(xhttp.responseText);
    

    或者如果你正在获取 XML,我不确定你为什么要再次解析它,因为 XMLHttpRequest 对象会为你做这件事。只要它是有效的 XML 文档,它就应该与 responseXML 一起使用。

    myFunction(xhttp.responseXML);  
    

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2019-08-05
      • 2016-08-08
      • 2019-08-19
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多