views:

12

answers:

1

I'm working with the jQuery Form Plugin. I have my servers send 200 responses on success, which trips the success listener perfectly fine. Per the standard, 301 and 302 and transparently redirected by the browser. However, when the server returns, say a 401, the Form Plugin just silently dies. How can I apply a listener to non-200 responses?

+1  A: 

The XHR will have the responseStatus in it - you can use the complete handler instead of 'success' and check that property of the xhr.

$.ajax({ // ...
        complete: function(xhr){
           if( xhr.responseStatus == 401 ) {
               alert( 'awww crap' );
           }
         }
      });
Dan Heberden
This seems to have worked formidably. I included `success: callback` with `complete: callback`. The complete even handler takes two arguments, the first being an XHR object (status available on `xhr.status`), the second being a string `"error"` or `"success"`.
Steven Xu
If you need to, you can append to the global ajax events too; the details will be in how you're building your application.
Dan Heberden