What happens when a network request request time during an AJAX request on ASP.NET MVC Action

As indicated in the title, I would like to know what happens when an AJAX request is sent to the controller, and during this time the network timeout occurs for several ms before the request is completed.

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

Given that a timeout only happens for a couple of ms, what consequences would this affect my AJAX request?

This is a continuation of the problem that we encounter in our application, as described in this CO> question , and I would like to know if they are somehow connected.

I have googled for similar questions, but haven't found anything useful.

Edit: Besides affecting AJAX, will this affect the behavior of the action method (server)?

+6
source share
3 answers

Regardless of whether the timeout occurs only within a few ms or more, the request will not be executed. The success callback function of your AJAX request will not be executed, and the request will end with the complete callback function. By default, all AJAX requests will have a timeout of 0 ms (unlimited), but it will hit the default browser timeout.

When the AJAX request expires, the error callback function will be called. The second argument to this function is a string describing the type of error, in which case it will be timeout . You can handle request timeouts by processing this callback function and optionally specifying a timeout value (if not specified, it works by default) in the body of the AJAX request:

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

In addition, you can also check if the request is programmed in the complete callback function (similar to the one shown above) by checking the second argument, which is a string, and will have a timeout value if the request was disconnected.

Also note:

The waiting period begins when $ .ajax is called; if several other requests are executed and there are no connections available in the browser, it is possible that the request will time out before it is sent.

Request timeouts usually either remain by default or are set as global defaults using $ .ajaxSetup (), rather than being overridden for specific requests with a timeout parameter.

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

More info: jQuery ajax documentation

+6
source

The request will fail, that is, it will enter the onError state of your AJAX request. The status code will be 0 because there is no response on the server to determine the real status code (for example, 200 OK or 500 Internal Server Error).

+3
source

In the event of a timeout, your callback will not be completed, so you will have to write a client-side error callback to solve such problems.

You must create an exception on the server side in the event of a timeout, so that it returns to the client as an error, which is the way you can handle the timeout.

0
source

All Articles