Ajax XML response is NULL
Ajax XML response is NULL
我试图做的是从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>
结束。
相关文章:
- contentWindow.document.body is null
- document.getElementById(..) is null
- jquery TypeError: Response is null
- iframe contentDocument and contentWindow is null
- document.querySelector(...) is null error
- getElementById is null?
- Sbt 0.13 ScriptEngine is Null for getEngineByName(“JavaScrip
- AngularJS "localStorage.getItem" is null in $resou
- javascript canvas is null
- Sails.create even when item is null?
- Jquery ajax POST response is null
- Google App Engine: User is null
- Microsoft.XMLHTTP documentElement is NULL
- TypeError: this.element is null, Rails with Judge gem
- FileReader.readAsArrayBuffer return is null
- TypeError: jasmine.getEnv().currentSpec is null
- gapi.auth.authorize: TypeError: cv(...) is null
- Error document.getElementById is null
- Javascript TypeError: document.getElementById(...) is null
- JavaScript :TypeError: document.getElementById(...) is null