当AJAX请求到ASP期间网络请求超时时会发生什么?净MVC行动

时间:2022-12-04 16:43:10

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?

考虑到超时只会发生几次ms,这会对我的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.

这是我们在应用程序中遇到的一个问题的延续,正如在这个问题中解释的那样,我想知道它们是否有某种关联。

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的影响之外,它是否会影响操作方法的行为(服务器)?

3 个解决方案

#1


6  

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. By default, all AJAX requests will have a timeout of 0ms (unlimited), but it will hit the default timeout of the browser.

无论超时是否仅发生在几个ms或更多,请求都将失败。AJAX请求的成功回调函数将不会被执行,而请求将以完整回调函数的执行结束。默认情况下,所有AJAX请求的超时时间都是0ms(无限制),但它将达到浏览器的默认超时时间。

When an AJAX request times out, the error callback function will be invoked. The second argument of this function is a string describing the type of error and in this case, it will have the value 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请求体中选择性地指定一个超时值(如果没有指定,可以使用默认值)来处理请求超时:

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

此外,您还可以通过检查第二个参数(即字符串)来检查请求是否在完整回调函数中超时(类似于上面所示),如果请求超时,它将具有值超时。

Also, note this:

另外,请注意:

The timeout period starts at the point the $.ajax call is made; 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.

超时时间从$开始。ajax调用;如果有其他几个请求正在进行中,并且浏览器没有可用的连接,那么在发送请求之前,请求可以超时。

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()设置为全局默认值,而不是使用超时选项覆盖特定请求。

I would suggest you to use an alternative HTTP/s traffic monitoring tool like fiddler to find the mystery behind the second request.

我建议您使用另一种HTTP/s流量监控工具,比如fiddler,来查找第二个请求背后的秘密。

More info: jQuery ajax documentation

更多信息:jQuery ajax文档

#2


3  

The request will "fail", meaning it will enter the onError state of your AJAX request. 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).

请求将“失败”,这意味着它将输入AJAX请求的onError状态。然后状态代码将为0,因为服务器没有响应来确定真正的状态代码(例如)。200 OK或500内部服务器错误)。

#3


0  

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.

您必须从服务器端提出一个异常,以防超时,以便它作为一个错误返回到客户端,这是处理超时的唯一方法。

#1


6  

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. By default, all AJAX requests will have a timeout of 0ms (unlimited), but it will hit the default timeout of the browser.

无论超时是否仅发生在几个ms或更多,请求都将失败。AJAX请求的成功回调函数将不会被执行,而请求将以完整回调函数的执行结束。默认情况下,所有AJAX请求的超时时间都是0ms(无限制),但它将达到浏览器的默认超时时间。

When an AJAX request times out, the error callback function will be invoked. The second argument of this function is a string describing the type of error and in this case, it will have the value 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请求体中选择性地指定一个超时值(如果没有指定,可以使用默认值)来处理请求超时:

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

此外,您还可以通过检查第二个参数(即字符串)来检查请求是否在完整回调函数中超时(类似于上面所示),如果请求超时,它将具有值超时。

Also, note this:

另外,请注意:

The timeout period starts at the point the $.ajax call is made; 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.

超时时间从$开始。ajax调用;如果有其他几个请求正在进行中,并且浏览器没有可用的连接,那么在发送请求之前,请求可以超时。

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()设置为全局默认值,而不是使用超时选项覆盖特定请求。

I would suggest you to use an alternative HTTP/s traffic monitoring tool like fiddler to find the mystery behind the second request.

我建议您使用另一种HTTP/s流量监控工具,比如fiddler,来查找第二个请求背后的秘密。

More info: jQuery ajax documentation

更多信息:jQuery ajax文档

#2


3  

The request will "fail", meaning it will enter the onError state of your AJAX request. 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).

请求将“失败”,这意味着它将输入AJAX请求的onError状态。然后状态代码将为0,因为服务器没有响应来确定真正的状态代码(例如)。200 OK或500内部服务器错误)。

#3


0  

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.

您必须从服务器端提出一个异常,以防超时,以便它作为一个错误返回到客户端,这是处理超时的唯一方法。