【问题标题】:Sum variables generated via Instagram API通过 Instagram API 生成的总和变量
【发布时间】:2015-09-12 14:41:56
【问题描述】:
$.ajax({
    url: "https://api.instagram.com/v1/users/xxxxxxxxx",
    dataType: 'jsonp',
    type: 'GET',
    data: { access_token: accessToken },
    success: function (data) {
        var followers_one = data.data.counts.followed_by;
        $('#assets .one').append(followers_one);
    }
});

$.ajax({
    url: "https://api.instagram.com/v1/users/xxxxxxxxx",
    dataType: 'jsonp',
    type: 'GET',
    data: { access_token: accessToken },
    success: function (data) {
        var followers_two = data.data.counts.followed_by;
        $('#assets .two').append(followers_two);
    }
});

我正在使用 Instagram API 提取几个帐户的关注者数量,但现在我需要汇总这些数量,但我遇到了一些问题。考虑到 ajax 调用,我应该如何处理?

【问题讨论】:

    标签: jquery ajax instagram


    【解决方案1】:

    您可以在两个 ajax 请求成功后使用promises(在您的情况下使用jQuery.when)执行单个函数。

    来自jQuery documentation

    jQuery.when 提供了一种基于一个或多个对象(通常是表示异步事件的延迟对象)执行回调函数的方法。

    例如:

    $.when($.ajax( "http://example1.com" ), $.ajax( "http://example2.com" ))
        .done( function successCallback(responseFromAjax1, reponseFromAjax2) { 
            // Success code goes here ...
        });
    

    你的情况是:

    $.when(
        $.ajax({
            url: "https://api.instagram.com/v1/users/xxxxxxxxx",
            dataType: 'jsonp',
            type: 'GET',
            data: { access_token: accessToken },
        }),
        $.ajax({
            url: "https://api.instagram.com/v1/users/xxxxxxxxx",
            dataType: 'jsonp',
            type: 'GET',
            data: { access_token: accessToken },
        })
    ).done(function (response1, response2) {
        // If you have troubles getting the actual data
        // put a breakpoint here to examine the structure of the responses 
    
        var followers_one = response1[0].data.data.counts.followed_by;
        var followers_two = response2[0].data.data.counts.followed_by;
        alert(followers_one + followers_two);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多