Friday, 22 August 2014

How to retrieve the responseXML from the xmlhttprequest? [duplicate]




This question already has an answer here:




Ok before you mark this as duplicate read my question please. I have some xml file on the server and I want to access it asynchronously, which I am able to do, the problem I am having is that I cant access the xml DOM returned by the xmlhttprequest. Here is my code.



function getXML(filename, callback) {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) { // request is done
if (httpRequest.status === 200) { // successfully
callback(httpRequest.responseXML); // we're calling callback method
}
}
};
httpRequest.open('GET', filename, true);
httpRequest.send(null);
}


Here I call the loadXML with a callback function and a filename as an argument.



getXML("testfile.xml", function (result) {
xmldoc = result;
console.log(subjectsDoc); //This works fine. I get the xml file.
});


Then I want to be able to access the XML document. But I get an error.



subjects = xmlDoc.getElementsByTagName("test"); //Error: "xmldoc is undefined"

No comments:

Post a Comment