I am trying to parse XML document from java script with the below code
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.responseXML != null) {
Caption(this.video, this.responseXML);
} else {
throw new Error("Can't read resource");
}
};
xhr.video = obj;
xhr.open("GET", "Br001.xml", true);
xhr.send("");
But I am getting status=0 and responseXML = NULL.
FollowUp:
After changing the onreadystatechange as below i am getting readyState=1 and status=0 and responsexml=NULL and getting only one callback
xhr.onreadystatechange = function () {
if (this.readyState == 4
&& this.status == 200) {
if (this.responseXML != null) {
Caption(this.video, this.responseXML);
} else {
throw new Error("Can't read resource");
}
}
};
1 Answer 1
readyState goes through multiple stages before the response is available. You have to wait for readyState to change to 4:
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.responseXML != null) {
Caption(this.video, this.responseXML);
} else {
throw new Error("Can't read resource");
}
}
};
It's also best to check status (e.g., to make sure it's 200 <= status < 300 (as 2xx are the "ok" responses), although your this.responseXML != null is probably good enough for this use.
4 Comments
file://... rather than http://... or https://...), most browsers disallow ajax calls from file://... resources.Explore related questions
See similar questions with these tags.