35

I'm a jQuery beginner. I'm trying to code a very simple using $.get(). The official documentation says:

If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method or.

As of jQuery 1.5, the .error() method of the jqXHR object returned by jQuery.get() is also available for error handling.

So, if all goes well, my callback function for success will be called. However, if the request fails, I would like to get the HTTP code :404, 502, etc and formulate a meaningful error message for the user.

However, since this is an asynchronous call I can imagine that I might have several outstanding. How would .ajaxError() know which request it corresponds to? Maybe it would be better to use the .error() method of the jqXHR object returned by jQuery.get()?

I am seeking an extremely simple code example. Perhaps the success routine calls Alert("Page found") and the failure routine checks for 404 and does an Alert("Page not found").

Update

The following page is extremely helpful: http://api.jquery.com/jQuery.get/

halfer
  • 19,824
  • 17
  • 99
  • 186
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

3 Answers3

67

You're right that you can use jQuery 1.5's new jqXHR to assign error handlers to $.get() requests. This is how you can do it:

var request = $.get('/path/to/resource.ext');

request.success(function(result) {
  console.log(result);
});

request.error(function(jqXHR, textStatus, errorThrown) {
  if (textStatus == 'timeout')
    console.log('The server is not responding');

  if (textStatus == 'error')
    console.log(errorThrown);

  // Etc
});

You can also chain handlers directly onto the call:

$.get('/path/to/resource.ext')
     .success(function(result) { })
     .error(function(jqXHR, textStatus, errorThrown) { });

I prefer the former to keep the code less tangled, but both are equivalent.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • +1 Thanks for the help. But what if I have several requests active in parallel? How can I tell which error belongs to which? – Mawg says reinstate Monica Feb 19 '11 at 23:30
  • 1
    The jqXHR object passed into the error handler will contain information about the original request that raised the error. Depending on your server-side implementation, errorThrown will also contain more detail about what went wrong. – Dave Ward Feb 19 '11 at 23:52
  • +1 Thanks again. So much to learn, alas. Btw, how do I pass CGI parameters to the URL. E.g google.com?serach="pizza" – Mawg says reinstate Monica Feb 20 '11 at 00:53
  • 1
    If you pass a JavaScript object as `$.get()`'s second parameter, it will URL encode that data and append it to the URL. For your example, you'd use `$.get('/path/to/resource.ext', { search: 'pizza' });` to add the parameter. – Dave Ward Feb 20 '11 at 03:19
  • 8
    jQuery now uses .done and .fail instead of .success and .error which have both been deprecated but will probably work for some time. http://api.jquery.com/jQuery.get/ – Jazzepi Oct 27 '13 at 13:19
  • 1
    @Jazzepi is right, but there's also this alternative https://stackoverflow.com/questions/41169385/http-get-success-is-not-a-function – Marcus Vinicius Pompeu Jan 01 '18 at 06:28
33

As of jQuery 3.0 .success() and .error() are deprecated.
You can use .done() and .fail() instead

$.get( url )
    .done(function( data, textStatus, jqXHR ) {
        console.log(data);
    })
    .fail(function( jqXHR, textStatus, errorThrown ) {
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown );
    });

Source: https://api.jquery.com/jQuery.ajax/

stallingOne
  • 3,633
  • 3
  • 41
  • 63
18

.get() is just a synonym for .ajax() with a number of options pre-set. Use ajax() to get the full range of options, including the error callback.

$.ajax({
type: "GET",
url: "test.htm",
error: function(xhr, statusText) { alert("Error: "+statusText); },
success: function(msg){ alert( "Success: " + msg ); }
}
);
Pekka
  • 442,112
  • 142
  • 972
  • 1,088