How to catch a 400 response in jQuery getJSON call

Muhammad Usman picture Muhammad Usman · Nov 14, 2012 · Viewed 16.1k times · Source

In jQuery I want to fetch some data from facebook using the $.getJSON() method, but if the token is invalid, Facebook is returning the 400 status. How I can catch the error in $.getJSON() instead of $.ajax()?

Answer

Elby picture Elby · Nov 14, 2012

I think this will work for you

$.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("success 2"); })
.error(function() { alert("error occurred "); })
.complete(function() { alert("Done"); });