title: What to do when an Ajax call fails because user login/session becomes invalid
body: <p>I have several web pages which require the user to be logged in.  These pages often make Ajax calls using jQuery.  When an Ajax call is made, the server is able to identify (via a session cookie) whether or not the user is logged in.  This is all pretty standard / basic stuff.</p>
  
  <p>My problem is that sometimes the session becomes invalid, even though the page is still up in the browser.  For example, the user logs in to the site, does some work - then goes away for a few hours.  Upon returning, the user's browser is still on the page and the user tries something which makes an Ajax call.  This Ajax call now fails because the user's session has timed out.  Another example is with multiple devices.  The user browses at his/her desk and leaves the browser up, and then the user for whatever reason logs in to the site with another machine or device.  This second device invalidates the first session (only one log in per user ID is allowed at this particular company).  Upon returning to the first box the user tries an Ajax operation which fails.</p>
  
  <p>Note that it's not security I'm worried about - the system works properly in this regard, ensuring that a user has a valid session when trying to make an Ajax call.  The problem is that it's not handled gracefully when the user's session has timed-out.  If the user did a lot of work (say filled in a long form) in one of these cases, and then tried to save the form using Ajax, the save would fail.  The only thing the user can do is to login to the system in another tab, and then go back to the tab that failed and try again.  Not very elegant.</p>
  
  <p>This must be a common problem but I'm not sure of a solution.  Ideally what I want is this: if an Ajax call fails due to the user not having a valid session, pop up a dialog box allowing the user to log on, then automatically re-try the action.</p>
  
  <p>I have, I don't know, maybe 50 Ajax calls in my client code.  Any way to do this globally? It'd be nice if there were some sort of easy jQuery solution to it.  What have people tried?</p>