我想要做的是从 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>