最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to catch a 400 response in jQuery getJSON call - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Nov 14, 2012 at 6:17 Jim O'Neil 23.8k7 gold badges45 silver badges69 bronze badges asked Nov 14, 2012 at 6:13 Muhammad UsmanMuhammad Usman 10.9k22 gold badges74 silver badges109 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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

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

Use the complete(jqXHR, textStatus) function callback and investigate the response and show the approprite message to the user.

发布评论

评论列表(0)

  1. 暂无评论