gpt4 book ai didi

javascript - Ajax XML 响应为 NULL

转载 作者:行者123 更新时间:2023-12-02 19:38:40 25 4
gpt4 key购买 nike

我想做的是从ajax请求中获取XML,以便从中提取数据(使用DOM,这并不重要)。不,我确实知道 ajax 工作得很好,因为如果我尝试获取 AjaxRequest.responseText,它工作得很好。我收到的错误消息是这样的:

“null”不是对象(评估“resturantsCategoriesAjaxXML.getElementsByTagName”)

当我尝试将responseXML写入日志时,我得到的只是空值,就像它尚未定义一样。我已经包含了处理 AJAX 的完整 JS(对奇怪的变量名称感到抱歉,我只是不想更改它们,以防问题是拼写错误),以及它正在获取的 XML(简单 XML)。非常感谢您的帮助:D

Java 脚本:

resturantsCategoriesAjaxRequestAdress = "testFiles/categoriesAjax.xml";
resturantsCategoriesAjaxRequest = new XMLHttpRequest();
resturantsCategoriesAjaxRequest.onreadystatechange = function(){
if(resturantsCategoriesAjaxRequest.readyState == 4){
resturantsCategoriesAjaxXML = resturantsCategoriesAjaxRequest.responseXML;
console.log(resturantsCategoriesAjaxRequest.responseXML);
categoriesArray = resturantsCategoriesAjaxXML.getElementsByTagName("category");
categoriesArraylenght = categoriesArray.length;
alert(categoriesArraylenght);
categoriesArrayIritationControl = 0;
while (categoriesArraylenght >= categoriesArrayIritationControl) {
categoryName = categoriesArray[categoriesArrayIritationControl].getAttribut("name");
//create new <li> object
//add to ctegories menu on restrants page
alert(categoryName);
}
}
}
resturantsCategoriesAjaxRequest.open("GET", resturantsCategoriesAjaxRequestAdress, true);
resturantsCategoriesAjaxRequest.overrideMimeType("text/xml");
resturantsCategoriesAjaxRequest.send();
console.log(resturantsCategoriesAjaxRequest.readyState);

XML:

<?xml version='1.0'?>
<category name="Fast_Food" id="1120"></category>
<category name="Chinese" id="1108"></category>
<category name="Italian" id="1230"></category>

最佳答案

我认为您的文档不是正确的 xml 文档,您需要上面的父标记 "<category>"

关于javascript - Ajax XML 响应为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com