AJAX和Facebook API

问题描述 投票:0回答:1

假设我想用这个:

$.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)

javascript ajax facebook-graph-api
1个回答
0
投票

如果请求因某种原因失败,请添加要调用的错误函数

$.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');
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.