I have an AJAX request running a PHP script and I want to handle situations were the request returns state 500.
My code is working on Chrome and Firfox, but on IE9, if I add a condition if(httpRequest.status==500)
I get this JavaScript error:
Could not complete the operation due to error c00c023f
.
I have a loading image on the page, and because of this error I'm not able to replace the image, and the page seems to keep loading. Just as quoted here:
Especially if you're displaying a "loading animation" or anything similar while the request is being executed. The result in my case was that it seemed as the page never stopped loading
First, here is my AJAX call code:
function sendRequest(url, params, divToChange)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
var httpRequest = new XMLHttpRequest();
}
else // code for IE6, IE5
{
var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
//open the request and prepare variables for the PHP method being activated on server side//
httpRequest.open("POST", url, true);
//Set request headers for POST command//
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
if(divToChange != "false")
{
httpRequest.onreadystatechange=function()
{
if(httpRequest.readyState==4 && httpRequest.status==200)
{
document.getElementById(divToChange).innerHTML=httpRequest.responseText;
eval(document.getElementById("runscript").innerHTML);
}
else if (httpRequest.status==500)
{
document.getElementById(divToChange).innerHTML=httpRequest.responseText;
//eval the lang variable from the php script.
eval(document.getElementById("runscript").innerHTML);
if(lang == "en")
{
document.getElementById(divToChange).innerHTML='<b> An error accured, please try again later</b>';
}
else
{
document.getElementById(divToChange).innerHTML='<b> שגיאה התרחשה, בבקשה נסה שנית מאוחר יותר</b>';
}
}
}
}
httpRequest.send(params); //sending the request to the server//
}
I've searched about it and found this post on stack. From there, I've found this quote:
Just before you call the XmlHttpRequest.abort() method, set a flag on it, this is how I did it:
XmlHttpRequest.aborted = true;
XmlHttpRequest.abort();
In the onreadystatechanged handler, the first lines of code in my case is:
if (xmlHttpRequest.aborted==true) {
stopLoadAnimation();
return;
}
But in my case, I dont even call XmlHttpRequest.abort()
, so why do I get the error for the first place ?
By the way, if I remove all the condition else if (httpRequest.status==500)
and all of the code in it, I wont get the error, but then again, I wont be able to display message on 500 error.