PHP スクリプトを実行する AJAX リクエストがあり、リクエストが状態 500 を返す状況を処理したいと考えています。
私のコードは Chrome と Firfox で動作していますが、IE9 で条件を追加すると、次のif(httpRequest.status==500)
JavaScript エラーが発生します。
Could not complete the operation due to error c00c023f
.
ページに読み込み中の画像がありますが、このエラーのために画像を置き換えることができず、ページは読み込み続けているようです。ここで引用されているように:
特に、リクエストの実行中に「読み込み中のアニメーション」などを表示している場合。私の場合の結果は、ページの読み込みが停止しないように見えたということでした
まず、私の 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='<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//
}
私はそれについて検索し、スタックでこの投稿を見つけました。そこから、私はこの引用を見つけました:
XmlHttpRequest.abort() メソッドを呼び出す直前に、それにフラグを設定します。これが私が行った方法です。
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 エラーでメッセージを表示できなくなります。