2012-05-19 113 views
0

我想要做的是从ajax请求中获取XML以便从中提取数据(使用DOM,而不是它很重要)。不,我知道ajax工作得很好,因为如果我尝试获取AjaxRequest.responseText,它就可以正常工作。那我收到错误消息说:Ajax XML响应为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> 
+0

你错过了xml中的根元素。在这种形式下它是无效的。也许这就是responseXML为null的原因。 – Andreas

+0

Dittos到Andreas的回应。让您的服务器端应用程序将标签中的文本包装起来。例如“ stuffyoudonow”。不是一个坏习惯。 –

回答

1

您的文档是不正确的XML文档,我认为,你需要上面的父母标签"<category>"

1

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

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