andrewlocatelliwoodcock

Thoughts on Software

AJAX post from window unload event not reaching controller

with one comment


I hit an interesting problem today: an AJAX post sent using jQuery’s $.ajax method was failing to reach the controller action method but only intermittently. The kind of problem guaranteed to drive me nuts until I’ve figured it out …

What was happening was that I was building a queue that I then wanted to post to a controller once the queue reached a certain size or once the user navigated away from the current page. The page navigation feature was implemented using jQuery’s window unload event:

$(window).unload …

The postQueue function was always being hit whether it was called from the queue manager or the unload event but it was only managing to correctly post to the controller if called from the queue manager … a half day’s work was fast turning into one and a half days …

Turns out that the issue was that you cannot post asynchronously from a function called from the unload event because there is no longer a callback function in scope even if you are not actually using a callback handler …

The solution is to make sure that you only post synchronously from within the unload event by setting the async property of the ajax object to false.

Written by andrewlocatelliwoodcock

May 13, 2011 at 17:30

Posted in ASP.Net MVC, javascript

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. lkafle

    July 5, 2013 at 05:07


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: