8-bit mate
8-bit mate

Reputation: 85

Ajax XML response is NULL

What I am trying to do is to get the XML from the ajax request in order to extract data out of it (using the DOM, not that it matters). No I do know that the ajax works just fine because if i try and get the AjaxRequest.responseText, it works alright. The error message that i am Getting is saying that:

'null' is not an object (evaluating 'resturantsCategoriesAjaxXML.getElementsByTagName')

and when i try to write the responseXML to the log, all i get is null, like it hasn't been defined. I have included the full JS that handles the AJAX (sorry for the weird variable names, I just didn't want to change them in case the problem is a typo), and the XML that it is fetching (simple XML). Thanks a lot for your help :D

Java Script:

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>

Upvotes: 0

Views: 934

Answers (2)

Nealbo
Nealbo

Reputation: 517

You need a root element that encapsulates your other nodes.

Perhaps <categories> closing with </categories> containing all of your <category> nodes.

Upvotes: 1

Sebas
Sebas

Reputation: 21522

Your document is not a proper xml document i think, you need a parent tag above "<category>"

Upvotes: 2

Related Questions