AJAX 和 Facebook API

AJAX and the Facebook API

假设我想使用这个:

$.ajax({
  url: apiUrl,
  success: function(result) {
    $.each(result, function(key, val) {
      var shareCount = val["share"]["share_count"];
      if (shareCount > 0) {
        setTimeout(function() {
        // Do something
        }, 0000);
      } else {
        setTimeout(function() {
        // Do something else
        }, 0000);
      }
    });
  }
});

这会从 Facebook 获得点赞数。现在,如果 API 已关闭/访问者无法访问它(不太可能,但如果会怎样)或返回类似以下的错误怎么办:

{
 "error": {
    "message": "(#4) Application request limit reached",
    "type": "OAuthException",
    "is_transient": true,
    "code": 4,
    "fbtrace_id": ""
  }
}

如果发生任何一种情况,我将如何显示错误消息?

error: function(result)

添加一个错误函数,当请求由于某种原因失败时被调用

$.ajax({
  url: apiUrl,
  success: function(result) {
    $.each(result, function(key, val) {
      var shareCount = val["share"]["share_count"];
      if (shareCount > 0) {
        setTimeout(function() {
        // Do something
        }, 0000);
      } else {
        setTimeout(function() {
        // Do something else
        }, 0000);
      }
    });
  },
  error: function(error) {
    // Do something with the error message
    alert('There was a problem fetching likes from facebook');
  },
});