Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .get error response function?

Tags:

jquery

Thanks to StackOverflow, I managed to get the following code working perfectly, but I have a follow up question.

$.get('http://example.com/page/2/', function(data){    $(data).find('#reviews .card').appendTo('#reviews'); }); 

This code above enabled my site to fetch a second page of articles with a Load More button in WordPress. However when the site runs out of pages/results, I'm running into a small issue. The load more button remains in place. It will keep trying to fetch data even if no pages are remaining.

How can I tweak this command so that I can show a visual response if the .get request fails or is a (404) not found page for instance?

If someone could help me out with a simple example of even an alert("woops!"); that would be awesome!

Thanks!

like image 300
Sahas Katta Avatar asked Oct 31 '10 08:10

Sahas Katta


People also ask

How can AJAX call error be resolved?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

How can we handle exception handling in AJAX?

Example: We are going to see how to use AJAX fail() methods to handle the error in the HTTP requests. The fail() callback takes 3 parameters where the first parameter is a JSON error object, the second parameter is given a reason in text format and the last parameter is for the error thrown by the HTTP request.

What is AJAX error function?

The ajaxError() method in jQuery is used to specify function to be run when an AJAX request fails. Syntax: $(document).ajaxError( function(event, xhr, options, exc) ) Parameter:: This method accepts single parameter function which is mandatory.


2 Answers

$.get does not give you the opportunity to set an error handler. You will need to use the low-level $.ajax function instead:

$.ajax({     url: 'http://example.com/page/2/',     type: 'GET',     success: function(data){          $(data).find('#reviews .card').appendTo('#reviews');     },     error: function(data) {         alert('woops!'); //or whatever     } }); 

Edit March '10

Note that with the new jqXHR object in jQuery 1.5, you can set an error handler after calling $.get:

$.get('http://example.com/page/2/', function(data){      $(data).find('#reviews .card').appendTo('#reviews'); }).fail(function() {     alert('woops'); // or whatever }); 
like image 174
lonesomeday Avatar answered Sep 26 '22 09:09

lonesomeday


If you want a generic error you can setup all $.ajax() (which $.get() uses underneath) requests jQuery makes to display an error using $.ajaxSetup(), for example:

$.ajaxSetup({   error: function(xhr, status, error) {     alert("An AJAX error occured: " + status + "\nError: " + error);   } }); 

Just run this once before making any AJAX calls (no changes to your current code, just stick this before somewhere). This sets the error option to default to the handler/function above, if you made a full $.ajax() call and specified the error handler then what you had would override the above.

like image 36
Nick Craver Avatar answered Sep 23 '22 09:09

Nick Craver