responseXML is null, javascript

210 views Asked by At

presentation.htm:

if(!system.ie) {
    try {
        DescXMLDocument = new XMLHttpRequest();
        DescXMLDocument.async = false;
        DescXMLDocument.onreadystatechange=checkDescXMLload;
        DescXMLDocument.open("get", "description.xml", true);
        DescXMLDocument.send(null);
    }
}

tacore.js:

if (DescXMLDocument.readyState == 4 && DescXMLDocument.status == 200) {
    alert("Error");
} else {
    document.getElementById('progressCount').innerHTML="��������o";
    document.getElementById('progressDiv').style.display="none";
    anodes=DescXMLDocument.responseXML.documentElement.childNodes;
    document.getElementById('zback').innerHTML = anodes[0].text;
    document.getElementById('coursepicture').innerHTML = anodes[1].text;
    document.getElementById('chaptername').innerHTML = anodes[2].text;
    document.getElementById('buttonsdiv').innerHTML = anodes[3].text;
    document.getElementById('generaldescription').innerHTML = anodes[4].text;
    document.getElementById('generalhelp').innerHTML = anodes[5].text;
    document.title=document.getElementById('chaptername').innerText;
}

I get following get error:

DescXMLDocument.responseXML is null

Does anyone know how to fix it?

1

There are 1 answers

1
Shilly On

Your 'checkDescXMLload' is backwards. Your xml will have arrived when readyState === 4 and status === 200, not the other way around. As written, you will try reading the xml on readyState 1,2 and 3 as well, when it hasn't arrived yet. Just switch around the if else and move the error alert elsewhere.

checkDescXMLload = function() {
    if (DescXMLDocument.readyState === 4) {
        if (DescXMLDocument.status === 200) {
            ...... /* your response handler */
            anodes = DescXMLDocument.responseXML.documentElement.childNodes;
            ...... /* your response handler */
        }
        else {
            alert('error');
        }
    }
}

As Anik says, your xml will only be ready to read once you reach state 4 and status 200.

If you want to do things on readystate 0,1,2 and 3 as well, you can indeed use the 'switch case'. If you dont want to do anything with the other readystates, you can just use DescXMLDocument.onload() instead of DescXMLDocument.onreadystatechange, which will work in (almost?) all modern browsers.