ajax请求后浏览器将等待多长时间? [英] How long will the browser wait after an ajax request?

查看:552
本文介绍了ajax请求后浏览器将等待多长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器回答请求之前,浏览器需要等待多长时间才能显示错误?这段时间可以无限吗?

How long can the browser wait before an error is shown before server answers for request? Can this time be unlimited?

推荐答案

如果您使用的是jQuery $ .ajax调用,则可以设置超时属性来控制请求以超时状态返回之前的时间。超时以毫秒为单位设置,因此只需将其设置为很高的值即可。您也可以将其设置为0(表示无限),但我认为应该设置一个较高的值。

If you are using a jQuery $.ajax call you can set the timeout property to control the amount of time before a request returns with a timeout status. The timeout is set in milliseconds, so just set it to a very high value. You can also set it to 0 for "unlimited" but in my opinion you should just set a high value instead.

注意:无限是实际上是默认值,但是大多数浏览器都有默认超时值。

Note: unlimited is actually the default but most browsers have default timeouts that will be hit.

由于超时而返回ajax调用时,它将返回错误状态您可以根据需要使用单独的案例处理超时。

When an ajax call is returned due to timeout it will return with an error status of "timeout" that you can handle with a separate case if needed.

因此,如果您想将超时设置为3秒,并在此处处理超时,请参见以下示例:

So if you want to set a timeout of 3 seconds, and handle the timeout here is an example:

$.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请求后浏览器将等待多长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆