javascript - 无法读取未定义的属性“childNodes”,但列表有效
问题描述
我有以下代码应该从 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
(结构完全正确)会导致在浏览器中检测到错误
未捕获的类型错误:无法读取 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>
我已经检查并找到了这篇文章,但没有犯该帖子中指出的错误。
编辑
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,请告诉我,但这是一个非常大的级联。
感谢你目前的帮助!
解决方案
好的,我仍然会很感激任何其他输入,但据我所知,除了在不同的计算机上运行它之外,我没有改变任何东西。并且在另一台计算机上运行它后重新启动我的电脑它也可以工作......我的想法是这是我的本地主机服务器的问题,它在重新启动时自行解决。?
谢谢康纳
推荐阅读
- objective-c - 在另一个带有 XCode GUI Builder 的 XIB 中使用 NSViewController 组件?
- python - 即使使用线程,kivymd MDProgressBar UI 也不会更新
- flutter - 带有地图和信息面板的应用程序屏幕应在按钮单击或向下滑动时隐藏和显示面板
- python - BS4:在 Python 中遍历页面返回相同的结果
- go - Go + yaml.Unmarshal + maps - 如何覆盖
- ms-access - MS ACCESS 中基于 Fifo 的销售
- ios - iOS UICollectionView ConsoleLog 当Item Width大于CollectionView宽度时的注意事项
- vba - 将格式化的 Excel 数据透视表插入 Word
- node.js - 如何将变量发送到中间件
- python - 在while循环的if语句中使用嵌套的try / except