Javascript – How to capture the 500 error message using jquery

ajaxjavascriptjquery

How can i capture the 500 error message using jquery? I want to keep on checking for the 500 error message for sometime until it changes and time out after 50 sec.

I used the code below to try to capture and check the 500 error message but it doesnt seem to catch the 500 error message. I can see it in the firebug

$.ajax({
    statusCode: {
        500: function() {
            alert(" 500 data still loading");
            console.log('500 ');
        }
    }
}); 

Best Answer

Dispite the accepted answer mentioned by @Danny, you can also do this in newer versions of jQuery.

var xhr = $.ajax({
    url: "somewhere"
});

xhr.fail(function(xhr, textStatus, error) {
    // Error handling stuff here ...
});

See Deferred Object.