Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser waits for ajax call to complete even after abort has been called (jQuery)

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.

like image 957
Todd Menier Avatar asked Jun 02 '09 20:06

Todd Menier


2 Answers

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.

like image 37
acdameli Avatar answered Sep 30 '22 04:09

acdameli


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.

like image 90
Todd Menier Avatar answered Sep 30 '22 02:09

Todd Menier