20

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?

asked Jan 14, 2011 at 1:39

5 Answers 5

58
+100

you could do

$.post("/myhandler", { value: 1 }, function(data) {
 alert(data);
}).fail(function(){ 
 // Handle error here
});

fail will be called if theres an error

answered Mar 7, 2013 at 21:35
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for fail function. it does the job! You can use it like this: .fail(function(e){ if(e.status == 404){ // ... } else{ // ... } });
Older code with .error() wasn't catching anything. Simply switching it to fail() did the trick.
This should be the accepted answer, as it directly answers the question, rather than asking everyone to use a different function.
25

use error handler on $.ajax()

$.ajax({
 url: "/myhandler", 
 data: {value: 1},
 type: 'post',
 error: function(XMLHttpRequest, textStatus, errorThrown){
 alert('status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
 },
 success: function(data){}
});

demo

Arseny
5,1774 gold badges24 silver badges24 bronze badges
answered Jan 14, 2011 at 1:47

2 Comments

So is this not possible with $.get?
@Reigel Beautiful! Didn't realize the load callback has a Status parameter.
6

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.

$(selector).ajaxError(function(event, xhr, ajaxOptions, errorThrown) {
 // handle ajax error here
});
answered Jan 14, 2011 at 2:11

Comments

2

Use $.ajax instead and use the error callback.

http://api.jquery.com/jQuery.ajax/

answered Jan 14, 2011 at 1:44

Comments

1

Try using $.ajaxSetup() , stausCode option

$.ajaxSetup({
 statusCode : {
 // called on `$.get()` , `$.post()`, `$.ajax()`
 // when response status code is `404`
 404 : function (jqxhr, textStatus, errorThrown) {
 console.log(textStatus, errorThrown);
 }
 }
 });
// $.get("/path/to/url/");
// $.post("/path/to/url/");
// $.ajax({url:"/path/to/url/"})
answered Oct 27, 2015 at 5:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.