无法读取未定义的属性“ childNodes”,但列表有效

时间:2019-03-18 13:31:23

标签: javascript xml xmlhttprequest

我有以下代码,应从xml文件中的每个父节点返回两个子节点值。

<?php session_start(); ?>
<html>

<head>
  <title> ADM Asset Availability </title>
  <!--  <meta http-equiv="refresh" content="10"> -->
</head>

<body onload="AssetUpdate()">

  <script type="text/javascript">
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        AssetUpdate(this);
      }
    };
    xhttp.open("GET", "Assets.xml", true);
    xhttp.send();

    function AssetUpdate(xml) {
      var n, x, i, txt, xmlDoc;
      xmlDoc = xml.responseXML;
      txt = "";
      x = xmlDoc.getElementsByTagName("Asset");

      for (i = 0; i < x.length; i++) {
        var txt1 = "<dvi class='rowContainer'>";
        var txt2 = x[i].getElementsByTagName("AssetName")[0].childNodes[0].nodeValue;
        var txt3 = "-";
        var txt4 = x[i].getElementsByTagName("Live")[0].childNodes[0].nodeValue;
        var txt5 = "</div>";
        txt += txt1 + txt2 + txt3 + txt4 + txt5;
      }
      document.getElementById("Asset").innerHTML = txt;
    };
  </script>

  <div id="Asset">

  </div>

</body>

</html>

txt2毫无问题地返回请求的值。但是,txt4(结构正确)会导致在

的浏览器中检测到错误。
  

未捕获的TypeError:无法读取AssetUpdate AssetPage.php:33上未定义的属性'childNodes'

我尝试了大约30种代码,循环和标记名称的变体,但仍然会导致相同的错误。

请帮助

这是供参考的xml;

<?xml version="1.0" ?><Assets>
<Asset>5
    <AssetID>1</AssetID>
    <AssetName>Laser</AssetName>
    <Live>Test</Live>
</Asset>
<Asset>7
    <AssetID>2</AssetID>
    <AssetName>Press</AssetName>
    <Live>Test1</Live>
</Asset>
<Asset>9
    <AssetID>3</AssetID>
    <AssetName>Fab</AssetName>
    <Live>Test2</Live>
</Asset>
<Asset>blue
    <AssetID>78</AssetID>
    <AssetName>Paint</AssetName>
    <Live>Test3</Live>
</Asset></Assets>

我已经检查并找到了这个post,但没有在该线程中发现错误。

编辑

Navigated to http://localhost/ADMApp/AssetPage.php 
AssetPage.php:24 Uncaught TypeError: Cannot read property 'responseXML' of undefined
at AssetUpdate (AssetPage.php:24)
at onload (AssetPage.php:6)
AssetUpdate @ AssetPage.php:24
onload @ AssetPage.php:6
AssetPage.php:34 HTMLCollection []length: 0__proto__: HTMLCollectionitem: ƒ 
item()arguments: (...)caller: (...)length: 1name: "item"__proto__: ƒ 
()apply: ƒ apply()arguments: (...)bind: ƒ bind()call: ƒ call()caller: 
(...)constructor: ƒ Function()length: 0name: ""toString: ƒ 
toString()Symbol(Symbol.hasInstance): ƒ [Symbol.hasInstance]()get arguments: 
ƒ ()set arguments: ƒ ()get caller: ƒ ()set caller: ƒ ()__proto__: 
Object[[FunctionLocation]]: <unknown>[[Scopes]]: Scopes[0][[Scopes]]: 
Scopes[0]length: (...)namedItem: ƒ namedItem()constructor: ƒ 
HTMLCollection()Symbol(Symbol.iterator): ƒ 
values()Symbol(Symbol.toStringTag): "HTMLCollection"get length: ƒ 
length()__proto__: Object
AssetPage.php:35 undefined
AssetPage.php:36 Uncaught TypeError: Cannot read property 'childNodes' of 
undefined
at AssetUpdate (AssetPage.php:36)
at XMLHttpRequest.xhttp.onreadystatechange (AssetPage.php:15)
AssetUpdate @ AssetPage.php:36
xhttp.onreadystatechange @ AssetPage.php:15
XMLHttpRequest.send (async)
(anonymous) @ AssetPage.php:19
AssetPage.php:19 XHR finished loading: GET 
"http://localhost/ADMApp/Assets.xml".
(anonymous) @ AssetPage.php:19

我不知道显示此错误代码的更好方法,所以如果这不正确,请教育我(下面是实际显示的图像)。

如果您需要我全面扩展HTMLCollection,请告诉我,但这是一个很大的级联。

Error Log

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好吧,我仍然会感激任何其他输入,但是据我所知,除了在另一台计算机上运行外,我没有做任何更改。在另一台计算机上运行我的PC后,重新启动它也可以正常工作。 我的想法是这是我的本地主机服务器出现的问题,该服务器在重新启动后自行解决了??

谢谢 康纳