[jQuery] Catching 401 errors to $.ajax calls

[jQuery] Catching 401 errors to $.ajax calls


Fellow earthicans,
I would like to use $.ajaxSetup to set up a global error handler that
knows how to deal with HTTP 401 codes ("Authentication required"). I
am enhancing a few tables with AJAX using jQuery but I made my backend
send back an HTTP error code 401 to force authentication if the user's
login session timed out. The idea was to redirect to the '/login' page
then.
This is what I tried:
$.ajaxSetup({
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert(errorThrown + '/' + textStatus + '/' +
XMLHttpRequest)
}
})
Unfortunately the errorThrown is "undefined" and textStatus just
contains "error". Of course I can just redirect to /login on any error
but that's not what I want.
When I tried ExtJS for my project (I've dumped that meanwhile and went
back to jQuery - don't worry) I was successful with this code:
Ext.Ajax.on({
requestexception: function(conn, response, options, e)
{
if (response.status == '401')
window.location.href = '/login';
}
});
Can anyone tell me how to properly catch 401 requests globally with
jQuery?
Kindly
Christoph