【问题标题】:Instagram API pagination: Storing returned dataInstagram API 分页:存储返回的数据
【发布时间】:2014-12-30 06:30:34
【问题描述】:

我一直在通过 Instagram API 修改分页;但是,对于如何通过递归调用存储返回的数据,我有点困惑。

我想将所有项目链接存储在一个数组中,但我当前的方法返回一个空数组。

var collected_objs = []; // Array to collect links (urls)

$(document).ready(function() {
    $('#fetch_followers').click(function() {
        pollInstagram('https://api.instagram.com/v1/users/3/media/recent/?callback=?&min_timestamp=1388563200&max_timestamp=1420099200&access_token={access_token}', 33);
    });
});

function pollInstagram(next_url, count) {
$.ajax({
    method: "GET",
    url: next_url,
    dataType: "jsonp",
    jsonp: "callback",
    jsonpCallback: "jsonpcallback",
    success: function(data) {

        $.each(data.data, function(i, item) {
            $("#log").val($("#log").val() + item.id + '\n');
            console.log("****************************");
            console.log("This " + JSON.stringify(item.link));
            console.log("****************************");
            $('#target').append('<div id="likes_info"><a href="'+item.link+'"><img src="'+item.images.thumbnail.url+'"/></a>'+"<p><span>"+item.likes.count+"</span></p></div>");

            collected_objs.push(item.link); // Adding urls to collected_objs -- Not Working
        });
        $("#log").val($("#log").val() + data.pagination.next_url + '\n');

        // If the next url is not null or blank:
        if( data.pagination.next_url && count <=50 ) {
            var n_url=data.pagination.next_url + "&callback=?";
            pollInstagram(n_url, ++count);
        }
    },
    error: function(jqXHR, textStatus, errorThrown) {
        //alert("Check you internet Connection");
        $("#log").val($("#log").val() + 'Error\n');
    }
});

}

【问题讨论】:

  • 定义“不工作”。我看不到您实际上是如何使用collected_objs 的结果的。您是否正确处理了 ajax 请求的异步性质?
  • 我的意思是说在调用之后变量collected_objs 没有被更新。这是我第一次使用异步调用,这解释了我对如何在分页完成后保持全局变量更新的无知。

标签: javascript api rest pagination instagram


【解决方案1】:

因为您对 instagram api 的 ajax 请求是异步执行的,所以您需要使用回调(作为参数传入)或返回 $.Deferred。我还建议查找有关处理异步 javascript 的资源。一开始可能很难理解,但那里有大量教程。

以下内容未经测试使用风险自负:)

回调路由

  function pollInstagram(next_url, count, callback) {
    $.ajax({
      //  ** snip **
      success: function (data) {
        // ** add data to collected objs **

        // pass our transformed data to callback
        callback(collectedObjs);
      }
      //  ** snip **
    })
  }

  // usage:
  pollInstaGram('url', count, function (objs) {
    // this callback is executed _after_
    // the ajax request completes
    // and passed data
    // do something with objs
  });

延期路线

  function pollInstagram(next_url, count) {
    // we return a jQuery defferred
    return $.ajax({
    //  ** snip **
    })
    .then(function (data) {
      // add data to collectedObjs
      return collectedObjs;
    })
    .fail(function () {
      // handle failure case
    })
  }

  // usage
  pollInstaGram('url', count).done(function (objs) {
    // done is executed after the deferred is resolved
    // with the transformed values from our `.then`
    // ** do something with objs **
  });

【讨论】:

  • 感谢@nicktomlin 的回复!我将读入异步 javascript,然后测试 sn-p。将报告我的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多