ajax ajax请求后浏览器会等待多久?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7297833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:03:42  来源:igfitidea点击:

How long will the browser wait after an ajax request?

ajaxrequestconnection-timeout

提问by Taras Lukavyi

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

在服务器响应请求之前,浏览器可以等待多长时间才能显示错误?这个时间可以无限吗?

回答by Tj Kellie

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.

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

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

注意:unlimited实际上是默认设置,但大多数浏览器都有默认超时设置。

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.

当由于超时而返回 ajax 调用时,它将返回“超时”错误状态,如果需要,您可以使用单独的情况处理。

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

所以如果你想设置 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
        }
    }
});?

回答by smassey

Yes and no. Yes the server can do it or be configured to do so, no the browsers (i dont know about version/distributor specifics) may have timeouts enabled.

是和否。是的,服务器可以这样做或被配置为这样做,没有浏览器(我不知道版本/发行商的细节)可能启用了超时。

There are 2 solutions though for achieving/emulating this over HTTP:

有两种解决方案可以通过 HTTP 实现/模拟:

  • If this is simple a long running script and you're waiting for results this isnt the way to go, you should instead do as previous poster mentioned and use async processing with server polling for the results, this would be a much more sure fire solution. For example: a thumbnail script from an image processor server side: the user uploads an image, the server immediately returns a 200 and a "Job ID". The client (javascript^^) can then use the JobID to request the job status/result.
  • If your goal is to have something like a realtime connection between browser and server (1 way connection, once the request is made by the browser no further info can be sent without using new requests (ajax^^)), this is called long polling/reverse ajax and can be used for real-time communication over http. There are several techniques using 2 long polled requests in parallel so that once one of them timeout the second one becomes the active and the first one attempts to reconnect.
  • 如果这是一个简单的长时间运行的脚本并且您正在等待结果,这不是要走的路,您应该按照前面提到的海报进行操作并使用异步处理和服务器轮询结果,这将是一个更确定的解决方案. 例如:来自图像处理器服务器端的缩略图脚本:用户上传图像,服务器立即返回 200 和“作业 ID”。客户端 (javascript^^) 然后可以使用 JobID 来请求作业状态/结果。
  • 如果您的目标是在浏览器和服务器之间建立实时连接(单向连接,一旦浏览器发出请求,在不使用新请求的情况下无法发送更多信息(ajax^^)),这称为长轮询/reverse ajax,可用于通过 http 进行实时通信。有几种技术并行使用 2 个长轮询请求,以便一旦其中一个超时,第二个变为活动状态,第一个尝试重新连接。

回答by Andy Davies

Can you explain a bit more about what you're trying to achieve - do you have a long running process on a server, do you want to change the settings on just a local machine or are you after a way to manage it for large numbers of users?

您能否详细解释一下您要实现的目标 - 您是否在服务器上有一个长时间运行的进程,您是否只想更改本地计算机上的设置,或者您是否想通过一种方法来管理它以进行大量操作?的用户?

How long the browser will wait depends on a number of factors e.g. where the timeout occurs - is it at the TCP level, the server or the local browser?

浏览器将等待多长时间取决于许多因素,例如超时发生的位置 - 是在 TCP 级别、服务器还是本地浏览器?

If you've got a long running process on a server and you want to update a webpage afterwards the typical way to handle it is to run the long process asynchronously and notify the client when it's complete e.g. have an ajax call that polls the server, or use HTTP 1.1 and serve out a notification stream to the client.

如果您在服务器上有一个长时间运行的进程,并且您想在之后更新网页,那么处理它的典型方法是异步运行长进程并在完成时通知客户端,例如有一个轮询服务器的 ajax 调用,或使用 HTTP 1.1 并向客户端提供通知流。

In either case it's still possible for the connection to be closed so the client will still need the ability to re-open it.

在任何一种情况下,连接仍然有可能被关闭,因此客户端仍然需要重新打开它的能力。

回答by ern0

I found, that in case of a normal (HTML page) request, browsers run to timeout after cca. 30 secs. It's important, because other participiants probably follows it: proxies, routers (do routers play in this game? I'm not sure). I am using 4 seclong server-side delay (if there's nothing to send to the client), and my AJAX client performs another HTTP request immediatelly (I am on local network, there's no internet lag). 4 sec is long enough to not to overload the server and network with frequented polls, and is short enough for the case, when somehow one poll falls out of the row which the client can't detect and handle.

我发现,在正常(HTML 页面)请求的情况下,浏览器会在 cca 之后运行超时。30 秒。这很重要,因为其他参与者可能会遵循它:代理、路由器(路由器在这个游戏中玩吗?我不确定)。我正在使用4 秒长的服务器端延迟(如果没有什么可以发送到客户端),我的 AJAX 客户端立即执行另一个 HTTP 请求(我在本地网络上,没有互联网延迟)。4 秒足够长,不会因频繁的轮询而使服务器和网络过载,并且对于这种情况来说足够短,当某个轮询以某种方式从客户端无法检测和处理的行中掉出时。

Also, there're other issues with comet (long HTTP request): browser's limit on number of simultaneous HTTP request, handling of client-side events (must sent to the server immediatelly), server/network down detection and recovery, multi user handling etc.

此外,comet 还存在其他问题(长 HTTP 请求):浏览器对同时 HTTP 请求数量的限制、客户端事件的处理(必须立即发送到服务器)、服务器/网络故障检测和恢复、多用户处理等等。