How do you catch Server Error or 404 page not found, when you use $.get or $.post ?
For example:
$.post("/myhandler", { value: 1 }, function(data) {
alert(data);
});
That will do absolutely nothing if there is a Server Error loading "/myhandler", or if it is not found.
How do you make it notify you if there is an error?
The other answers are nice and all, but there's alternative solutions, namely
.ajaxSetup
,.ajaxError
and other Ajax event handlers (check the ajaxSetup doc page for more info on the rest).For example, with
.ajaxError
you can setup a global handler of all your ajax errors from.post
,.get
,.getJSON
and.ajax
for a specific set of elements.Use
$.ajax
instead and use theerror
callback.http://api.jquery.com/jQuery.ajax/
use
error
handler on$.ajax()
demo
you could do
fail will be called if theres an error
Try using
$.ajaxSetup()
,stausCode
option