24

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?

4
  • 1
    Can you show an example of whats being returned and triggering the alert?
    – Matt
    Mar 22, 2011 at 9:17
  • 1
    it fires the "error" function because it didnt exist and you just made one :) Mar 22, 2011 at 9:38
  • @Bob - thanks - what's creating the error? :)
    – AP257
    Mar 22, 2011 at 18:09
  • In this case, you were creating a function called error. But what the real error is, I don't know because i don't have your files :) Thx for the mark, hope you'll find your error! Mar 22, 2011 at 19:06

5 Answers 5

28

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 );
    }
  });
11

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

9

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

0

Using jQuery 3.2.1:

$.getJSON('/api/feed/update', function (data) {
    console.log(data);
}).catch(function (jqXHR, textStatus, errorThrown) {
    console.error(jqXHR);
    console.error(textStatus);
    console.error(errorThrown);
});
-1

Please do the following. Pseduo code:

$.getJSON('/path/to_your_url.do?dispatch=toggle&'  + new Date().getTime(),    function(data, status, xhr){

    if( xhr.status == 200 )
      // all good and do your processing with 'data' parameter( your response)
    else { 
      //error - check out the values for only in chrome for 'console'
     console.log(xhr.status);
     console.log(xhr.response);
     console.log(xhr.responseText)
     console.log(xhr.statusText);
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.