Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch a 400 response in jQuery getJSON call

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()?

like image 715
Muhammad Usman Avatar asked Nov 14 '12 06:11

Muhammad Usman


2 Answers

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"); });
like image 94
Elby Avatar answered Sep 21 '22 18:09

Elby


The jquery Ajax docs offer two solutions, the first is the error function:

    error(jqXHR, textStatus, errorThrown)

which detects and reports textual portions of error messages for you, the other is the status code feature (on the same page). Here's the example usage from that page:

    $.ajax({
      statusCode: {
        404: function() {
          alert("page not found");
        }
      }
    });
like image 31
A.M. D. Avatar answered Sep 23 '22 18:09

A.M. D.