weixin_33681778 2018-09-12 04:43 采纳率: 0%
浏览 11

AJAX和Facebook API

Let's say I want to use this:

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

Which would get a like count from Facebook. Now what if the API is down / the visitor can't access it (unlikely, but what if) or throws back an error like:

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

How would I go about displaying an error message if either were to happen?

error: function(result)

  • 写回答

1条回答 默认 最新

  • weixin_33725239 2018-09-12 19:14
    关注

    Add an error-function to be called if the request fails for some reason

    $.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');
      },
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一个识别内容的自动化脚本程序
  • ¥15 anaconda虚拟python环境部署langchain-chatchat报错
  • ¥20 matlab有约束条件下的多元函数求最小值
  • ¥50 如何隐藏网页弹出框的url地址栏
  • ¥20 metropolis算法模拟二维ising模型来计算磁化强度,fortran
  • ¥15 uniapp-typescript-vue报错
  • ¥15 oracle强制关机以后报错01033
  • ¥15 给Chat with RTX添加语言模型时遇到问题
  • ¥15 oracle修复,怎么根据日志修复呀?
  • ¥15 使用Stable Diffusion时出现错误