简体   繁体   English

在对ASP.NET MVC Action的AJAX请求期间有网络请求超时时会发生什么

[英]What happens when there is network request time out during AJAX request to ASP.NET MVC Action

As stated in the title, i would like to know what happens when an AJAX request is sent to a controller action and, during this time, a network timeout happens for a few ms before the request is completed. 如标题中所述,我想知道将AJAX请求发送到控制器操作时会发生什么,并且在此期间,在请求完成之前几毫秒发生网络超时。

Reply from <Server IP>: bytes=32 time=31ms TTL=122
Request timed out
Reply from <Server IP>: bytes=32 time=28ms TTL=122

Considering the timeout happens only for a couple of ms, what effects would this have on my AJAX request? 考虑到超时仅发生几毫秒,这会对我的AJAX请求产生什么影响?

This is in continuation to a problem we are facing in our application as explained in this SO question and i would like to know if they are somehow related. 这是我们在我们的应用程序中遇到的问题的延续,正如在这个SO问题中所解释的那样,我想知道它们是否以某种方式相关。

I have googled for similar issues but couldn't find anything useful. 我搜索过类似的问题,但找不到任何有用的东西。

Edit: Apart from the impact on AJAX, would it affect the action method's behavior (server)? 编辑:除了对AJAX的影响外,它会影响动作方法的行为(服务器)吗?

Regardless of whether the timeout happens only for a couple of ms or more, the request will fail. 无论超时是否仅发生几毫秒或更长时间,请求都将失败。 The success callback function of your AJAX request will not be executed and the request will end with the execution of complete callback function. 您的AJAX请求的success回调函数将不会被执行,并且请求将以执行complete回调函数结束。 By default, all AJAX requests will have a timeout of 0ms (unlimited), but it will hit the default timeout of the browser. 默认情况下,所有AJAX请求都将具有0ms(无限制)的超时,但它将达到浏览器的默认超时。

When an AJAX request times out, the error callback function will be invoked. 当AJAX请求超时时,将调用error回调函数。 The second argument of this function is a string describing the type of error and in this case, it will have the value timeout . 此函数的第二个参数是一个描述错误类型的字符串,在这种情况下,它将具有值timeout You can handle request timeouts by handling this callback function and by optionally specifying a timeout value (if not specified, works on the default value) in the AJAX request body: 您可以通过处理此回调函数并通过在AJAX请求正文中指定超时值(如果未指定,对默认值起作用)来处理请求超时:

$.ajax({
    ...
    timeout: 5000, //specify the timeout value in milliseconds
    error: function(jqXHR, textStatus, errorThrown) {
        if(textStatus==="timeout") {
           //code to execute when timeout occurs
        } 
    }
});​

Additionally, you can also check if the request has timed out in the complete callback function (in a similar way as shown above) by checking the second argument which is a string and it will have the value timeout if the request was timed out. 此外,您还可以通过检查第二个参数是否为字符串来检查请求是否在complete回调函数中超时(以类似于上面所示的方式),如果请求timeout ,它将具有timeout值。

Also, note this: 另外,请注意:

The timeout period starts at the point the $.ajax call is made; 超时时间从$ .ajax调用点开始; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. 如果其他几个请求正在进行且浏览器没有可用的连接,则请求可能会在发送之前超时。

Request timeouts are usually either left at their default or set as a global default using $.ajaxSetup() rather than being overridden for specific requests with the timeout option. 请求超时通常保留为默认值或使用$ .ajaxSetup()设置为全局默认值,而不是使用timeout选项覆盖特定请求。

I would suggest you to use an alternative HTTP/s traffic monitoring tool like fiddler to find the mystery behind the second request. 我建议你使用像fiddler这样的替代HTTP / s流量监控工具来找到第二个请求背后的谜团。

More info: jQuery ajax documentation 更多信息: jQuery ajax文档

The request will "fail", meaning it will enter the onError state of your AJAX request. 请求将“失败”,这意味着它将进入AJAX请求的onError状态。 The status code will then be 0, since there is no response from the server to determine the real status code (eg. 200 OK or 500 Internal Server Error). 状态代码将为0,因为服务器没有响应来确定实际状态代码(例如200 OK或500内部服务器错误)。

In case of time-out your success callback wont execute so you have to write an error callback at the client side to handle such issues. 如果超时,您的成功回调将不会执行,因此您必须在客户端编写错误回调来处理此类问题。

You have to raise an exception from server side in case of time-out so that it will get back to the client as an error that is the one way you can handle the time-out. 如果超时,您必须从服务器端引发异常,以便它作为错误回到客户端,这是您可以处理超时的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM