30

I have some (potentially) long-running ajax calls that I would like to abort if the user navigates to another page. The following jQuery code calls abort on all pending XMLHttpRequest objects upon navigating away from the page:

$.ajaxSetup({
    beforeSend: function(xhr) {
        $(window).bind('beforeunload', function() {
            xhr.abort();
        });
    }
});

In a test case, I force a 10-second wait on the server-side operation being called. Using Firebug, I confirmed that the above code does indeed cause all pending ajax calls to halt immediately when I click any link on the page. However, the browser still waits the full 10 seconds before moving on to the next page. IE appears to exhibit the same behavior. Is this a known browser behavior? Is there anything I can do allow the user to navigate away from the page immediately in this situation? Thanks in advance.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173

7 Answers7

31

Thank you for your replies! It turns out I was completely wrong about this being a browser issue - the problem was on the server. ASP.NET serializes requests of the same session that require session state, so in this case, the next page didn't begin processing on the server until those ajax-initiated requests completed.

Unfortunately, in this case, session state is required in the http handler that responded to the ajax calls. But read-only access is good enough, so by marking the handler with IReadOnlySessionState instead of IRequiresSessionState, session locks are not held and the problem is fixed.

Hope this information proves useful to others.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173
  • The you have any references that explains this in more details? – Phil Sep 22 '14 at 23:47
  • @Phil, I found more information about this problem with ASP.NET here: http://johnculviner.com/asp-net-concurrent-ajax-requests-and-session-state-blocking/ – John Washam May 21 '15 at 21:09
  • Is it possible to avoid the wait if we are using the session state (read and edit)? – VikkyB Apr 04 '18 at 17:20
13

Regarding Todd's own answer to this question...

I just had this issue with PHP and the same solution would have worked. However I needed the information in the session. For PHP developers you can call session_write_close() to close and write out your session in the middle of the request. This will free up the session for the other requests.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
acdameli
  • 171
  • 1
  • 4
3

You might want to check a weird side effect of abort()

When the abort() method is used, the readystatechange event fires in Explorer and Mozilla. Worse, readyState = 4, which means that the average xmlhttp script assumes the data has been loaded correctly. This can give very weird effects.

documented here:

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html

Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
2

Are you sure that you are using an asychronous request? If the browser blocks during the entire request, you are using a synchronous request (async parameter is false)

fbuchinger
  • 4,494
  • 2
  • 30
  • 31
  • The server is actually stalling the navigation because the page it wants to navigate to uses the same session as the AJAX request which is running and holding on to the session. – grantwparks Oct 27 '17 at 23:29
1

The server issue is also the case with the Apache/Php server that I am working with. Removed the session_start on a script that did not need it (AJAX) and everything is working as expected. Thanks to Todd who pointed out a like problem!

Artistan
  • 1,982
  • 22
  • 33
0

Reference to the accepted answer:

http://improve.dk/optimizing-performance-programmatically-setting-readonlysessionstate/

About IRequiresSessionState

What this means is that, for a given session, only one request can execute concurrently. Any other requests, from that same session, will block, waiting for the session to be released

EvertonMc
  • 373
  • 1
  • 10
0

For ASP.NET MVC you can decorate the controller with the following [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)].

More information can be found here ... http://johnculviner.com/asp-net-concurrent-ajax-requests-and-session-state-blocking/

Caio Sant'Anna
  • 304
  • 4
  • 22