I have got multiple JavaScript ajax requests on my page. Now, on a particular ajax call (triggered by an onclick event), I get 3 error messages back to back. On checking for request.readyState I found that each time the error comes, it’s got ready states as 1, 2 and 3 respectively and then it works (readyState=4 obviously).
Here is the ajax request I am sending to the server:
function fetch(parameter){
var myrequest=new ajaxRequest()
myrequest.onreadystatechange=function(){
if (myrequest.readyState==4){ //This property is not equal to "4" for 3 calls
if (myrequest.status==200 || window.location.href.indexOf("http")==-1){
// Code to be executed for successful call
}
}
else{
alert("Error in Request. Request State is: " + myrequest.readyState);
}
}
myrequest.open("GET", url, true)
myrequest.send(null)
}
On triggering the onclick event, I get errors as:
Error in Request. Request State is: 1
Error in Request. Request State is: 2
Error in Request. Request State is: 3
What can be the problem with the request?
Those are XMLHTTPRequest (ajax) states where:
1 => loading
2 => loaded
3 => interactive
4 => ready
At 4, the ajax request is deemed complete and the returned data can be accessed.
You should just drop the
elsecondition.