如何检测ajax请求上的php服务器超时

时间:2022-10-14 20:51:40

Hey guys was hoping you could help me out.

嘿伙计们希望你能帮助我。

I want to know that suppose you did an ajax request to a page that runs PHP code. The page outputs some data (using flush() method or otherwise) but because of a 30second timeout or some other error, the php request ends.

我想知道,假设您对运行PHP代码的页面发出了ajax请求。页面输出一些数据(使用flush()方法或其他方式)但由于30秒超时或其他一些错误,php请求结束。

Now when the request ends, I want to find out about it on client side and restart the request.

现在,当请求结束时,我想在客户端找到它并重新启动请求。

i.e suppose I have something like this

即假设我有这样的东西

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.
    }

}

1 个解决方案

#1


0  

One strategy I used was to set a timer in JS, and then clear it if the call was successful.

我使用的一个策略是在JS中设置一个计时器,然后在调用成功时清除它。

var myTimer = setTimeout(stuffToDoOnFailure, 6000); // 6 secs

ajaxCall(function callBack() {
    clearTimeout(myTimer);
});

EDIT:

编辑:

Of course, if the ajax call succeeds after 6 secs, you might end up with both stuffToDoOnFailure and callBack being executed, so you want to handle that case somehow. That depends on your app, though.

当然,如果ajax调用在6秒后成功,你可能最终会同时执行stuffToDoOnFailure和callBack,所以你想以某种方式处理这种情况。这取决于你的应用程序。

#1


0  

One strategy I used was to set a timer in JS, and then clear it if the call was successful.

我使用的一个策略是在JS中设置一个计时器,然后在调用成功时清除它。

var myTimer = setTimeout(stuffToDoOnFailure, 6000); // 6 secs

ajaxCall(function callBack() {
    clearTimeout(myTimer);
});

EDIT:

编辑:

Of course, if the ajax call succeeds after 6 secs, you might end up with both stuffToDoOnFailure and callBack being executed, so you want to handle that case somehow. That depends on your app, though.

当然,如果ajax调用在6秒后成功,你可能最终会同时执行stuffToDoOnFailure和callBack,所以你想以某种方式处理这种情况。这取决于你的应用程序。