16

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 ');
        }
    }
}); 
dav_i
  • 27,509
  • 17
  • 104
  • 136
user244394
  • 13,168
  • 24
  • 81
  • 138
  • 3
    Take a look at the code used in [this question](http://stackoverflow.com/questions/6700822/jquery-how-to-get-the-http-status-code-from-within-the-ajax-error-method), and also [its accepted answer](http://stackoverflow.com/a/6700873/114359) – Daniel Miladinov Nov 30 '12 at 04:57
  • I have rolled back the question to show the original code. Please don't edit the meaning of questions. – dav_i Feb 13 '15 at 13:44

3 Answers3

11

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.

Vicary
  • 1,026
  • 1
  • 15
  • 35
6

Are you missing url in $.ajax like the one below

$.ajax({
    url: "/path to page",
    statusCode: {
        500: function() {
            alert(" 500 data still loading");
            console.log('500 ');
        }
    }
}); 
Murali N
  • 3,451
  • 4
  • 27
  • 38
  • I tried adding url to it too that didnt work.. it hungs up the page – user244394 Nov 30 '12 at 05:08
  • what do you mean by hungs up the page as i can see now your code should work now if the URL is correct – Murali N Nov 30 '12 at 05:10
  • the url is correct .. but when i add the url to ajax the page stop loading as I refresh the page after submiting. but if i remove the url i can see the 500 Internal server error on fire bug .. but i am unable to alert out the 500 error message – user244394 Nov 30 '12 at 05:28
  • in your code try to replace url: "somepage.html"; with this one url: "somepage.html", that should be , not ; – Murali N Nov 30 '12 at 05:32
  • updated "somepage.html"; with this one url: "somepage.html", - Thanks – user244394 Nov 30 '12 at 16:53
3

You can check the status in error of ajax post please check the below code.

 $.ajax({
        .....
        success: function (data) {

        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function (e, status) {

            if (e.status == 404)
                alert("404 error");
        }
    });

Thanks

Sridhar Narasimhan
  • 2,643
  • 2
  • 17
  • 25