getElementsbyTagName不是函数XMLHttpRequest

时间:2016-10-06 16:14:19

标签: javascript angular xmlhttprequest

我有问题。

我正在调用一个Web服务,它给了我一个XML响应,所以我使用了XMLHttpRequest。 现在我想访问一个节点中存储的一个值。

这是我的代码:

xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {

                        console.log("response : " + xmlhttp.responseXML.getElementsbyTagName("return")[0].childNodes[0].nodeValue);
                        ...

我确定return节点存在且包含某些内容,因为我的XML响应必须如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:carreNombreResponse xmlns:ns2="http://mathsutility.test.com/">
         <return>16.0</return>
      </ns2:carreNombreResponse>
   </soap:Body>
</soap:Envelope>

但问题是,当我的程序执行console.log行时,它会返回错误:

 TypeError: xml.getElementsbyTagName is not a function
    at XMLHttpRequest.xmlhttp.onreadystatechange...

我提到我使用的是Typescript和angular 2(但通常它不应该改变基本的js函数)。

有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

嗨Calliste它正在发生,因为xml不是DOMElement。 它只是一个xml数据类型

我建议您使用xml解析函数而不是使用DOM函数

Jquery有一个非常好的人来完成这项工作

它被称为

PaseXML https://api.jquery.com/jQuery.parseXML/