Ajax XML response is NULL

Ajax XML response is NULL

本文关键字:NULL is response XML Ajax      更新时间:2023-09-26

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

'null' 不是对象(评估 'resturantsCategoriesAjaxXML.getElementsByTagName'(

当我尝试将响应XML写入日志时,我得到的只是null,就像它没有被定义一样。我已经包含了处理 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>" 上方添加一个父标签

您需要一个封装其他节点的根元素。

也许<categories>以包含所有<category>节点的</categories>结束。