如果您使用jQuery $ .ajax调用,您可以设置timeout属性以控制请求返回之前的超时状态的时间量。超时设置为毫秒,因此只需将其设置为非常高的值。你也可以将其设置为0为“无限”,但在我看来,你应该只设置一个高的值。

注意:unlimited是actually the default,但大多数浏览器都有默认的超时时间。

当由于超时返回ajax调用时,它将返回错误状态“timeout”,您可以根据需要使用单独的大小写进行处理。

所以如果你想设置一个超时为3秒,并且处理超时这里是一个例子:

$.ajax({

url: "/your_ajax_method/",type: "GET",dataType: "json",timeout: 3000,//Set your timeout value in milliseconds or 0 for unlimited

success: function(response) { alert(response); },error: function(jqXHR,textStatus,errorThrown) {

if(textStatus==="timeout") {

alert("Call has timed out"); //Handle the timeout

} else {

alert("Another error was returned"); //Handle other error type

}

}

});​

更多推荐

ajax最长等待时间,在ajax请求后浏览器会等待多长时间?