Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: handle errors in getJSON()?

How do I handle 500 errors when using jQuery's getJSON?

There have been a couple of questions about error handling with getJSON() and JSONP, but I'm not working with JSONP, just ordinary JSON.

Another answer suggests using .ajaxSetup() before calling getJSON(), so I tried this:

$.ajaxSetup({
  "error":function() {   
    alert('Error!');
}});
$.getJSON('/book_results/', function(data) { # etc

But I find that the alert always triggers, even when the result is well-formed.

Any ideas?

like image 535
AP257 Avatar asked Mar 22 '11 09:03

AP257


People also ask

What is the difference between getJSON and Ajax in jQuery?

getJSON() is equal to $. ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error. So you were mostly right about the two being pretty much the same :).

What is jQuery getJSON?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

How jQuery read data from JSON file?

Projects In JavaScript & JQuery To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

What are the arguments of getJSON method?

It is a callback function that executes on the successful server request. It also has three parameters that are data, status, and xhr in which data contains the data returned from the server, status represents the request status like "success", "error", etc., and the xhr contains the XMLHttpRequest object.


3 Answers

The getJSON method does not natively return errors but you could dive into the xhr object that is returned as a parameter in the callback.

The getJSON method is a shorthand function for jQuery.ajax. Using jQuery.ajax you can easily achieve error handling:

  $.ajax({     url: 'http://127.0.0.1/path/application.json',     dataType: 'json',     success: function( data ) {       alert( "SUCCESS:  " + data );     },     error: function( data ) {       alert( "ERROR:  " + data );     }   }); 
like image 174
halfpastfour.am Avatar answered Oct 28 '22 15:10

halfpastfour.am


If you are using the jquery version 1.5 or higher you can use the new methods .success(function), .error(function) and .complete(function)

Example from http://api.jquery.com/jQuery.get/

// Assign handlers immediately after making the request, // and remember the jqxhr object for this request var jqxhr = $.get("example.php", function() {   alert("success"); }) .success(function() { alert("second success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); });  // perform other work here ...  // Set another completion function for the request above jqxhr.complete(function(){ alert("second complete"); }); 

Works perfect for me. I hope this helps

like image 34
Bibek Shrestha Avatar answered Oct 28 '22 15:10

Bibek Shrestha


you can see it on jquery api getJSON: http://api.jquery.com/jQuery.getJSON/

$.getJSON(url).done(function(data){
   $("#content").append(data.info);
})
.fail(function(jqxhr){
   alert(jqxhr.responseText);
});

//jquery1.5+ the fail callback will trigger when text is not the correct json string or any other fail solutions

like image 41
lee Avatar answered Oct 28 '22 17:10

lee