我有运行PHP脚本的AJAX请求,我想处理的情况是请求返回状态500IE9 AJAX错误:“无法完成操作由于错误c00c023f”
我的代码是工作在Chrome和Firfox,但在IE9上,如果我添加一个条件if(httpRequest.status==500)我得到这个JavaScript错误:
Could not complete the operation due to error c00c023f。
我在页面上有一个加载图像,并且由于此错误,我无法替换图像,并且页面似乎仍在加载。正如这里引述:
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
首先,这里是我的AJAX调用代码:
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=' An error accured, please try again later';
}
else
{
document.getElementById(divToChange).innerHTML=' שגיאה התרחשה, בבקשה נסה שנית מאוחר יותר';
}
}
}
}
httpRequest.send(params); //sending the request to the server//
}
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;
}
但在我而言,我不甚至称XmlHttpRequest.abort(),所以为什么我获得第一名的错误?顺便说一句,如果我删除所有的条件else if (httpRequest.status==500)和它的所有代码,我不会得到错误,但是,然后再次,我不能够显示消息500错误。