[英]How to detect php server timeout on ajax request?
我想知道假設您向運行 PHP 代碼的頁面發出了 ajax 請求。 該頁面輸出一些數據(使用flush() 方法或其他方式)但由於30 秒超時或其他錯誤,php 請求結束。
現在當請求結束時,我想在客戶端了解它並重新啟動請求。
即假設我有這樣的東西
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==3 && xmlhttp.status==200){
document.getElementById("A").innerHTML=xmlhttp.responseText;
}
else if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("A").innerHTML=xmlhttp.responseText;
//plus some additional code to end the process
}
else if(xmlhttp.status== SOMETHING /*WHAT DO I ADD HERE TO KNOW THAT THE SERVER HAS TIMED OUT OR SOMETHING SO I CAN RESTART THE XMLHTTP CYCLE */){
//code to resend xmlhttp request.
}
}
我使用的一種策略是在 JS 中設置一個計時器,如果調用成功則清除它。
var myTimer = setTimeout(stuffToDoOnFailure, 6000); // 6 secs
ajaxCall(function callBack() {
clearTimeout(myTimer);
});
編輯:
當然,如果 ajax 調用在 6 秒后成功,您可能最終會同時執行stuffToDoOnFailure
和callBack
,因此您希望以某種方式處理這種情況。 不過,這取決於您的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.