Javascript Instagram API分页:存储返回的数据
我一直在通过Instagram API修补分页;然而,对于如何通过递归调用来存储返回的数据,我有点困惑 我想将所有项目链接存储在一个数组中,但我当前的方法返回一个空数组Javascript Instagram API分页:存储返回的数据,javascript,api,rest,pagination,instagram,Javascript,Api,Rest,Pagination,Instagram,我一直在通过Instagram API修补分页;然而,对于如何通过递归调用来存储返回的数据,我有点困惑 我想将所有项目链接存储在一个数组中,但我当前的方法返回一个空数组 var collected_objs = []; // Array to collect links (urls) $(document).ready(function() { $('#fetch_followers').click(function() { pollInstagram('https:/
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');
}
});
}
收集的var_objs=[];//收集链接(URL)的数组
$(文档).ready(函数(){
$('fetch#u followers')。单击(函数(){
花粉图('https://api.instagram.com/v1/users/3/media/recent/?callback=?&min_timestamp=1388563200&max_timestamp=1420099200&access_token={access_token}',33);
});
});
函数pollInstagram(下一个url,计数){
$.ajax({
方法:“获取”,
url:next_url,
数据类型:“jsonp”,
jsonp:“回调”,
jsonpCallback:“jsonpCallback”,
成功:功能(数据){
$.each(data.data,function(i,item){
$(“#log”).val($(“#log”).val()+item.id+'\n');
console.log(“*************************************”);
log(“This”+JSON.stringify(item.link));
console.log(“*************************************”);
$(“#目标”).append(“+”“+item.likes.count+””);
collected_objs.push(item.link);//向collected_objs添加URL——不起作用
});
$(“#log”).val($(“#log”).val()+data.pagination.next_url+'\n');
//如果下一个url不是null或空:
如果(data.pagination.next_url&&count是因为对instagram api的ajax请求是异步执行的,则需要使用回调(作为参数传入)或者返回一个
$。延迟
。我还建议查找有关处理异步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 **
});
延迟路由
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 **
});
定义“不工作”。我看不出您实际上是如何使用
collected\u objs
的结果的。您是否正确地处理了ajax请求的异步性质?我的意思是,变量collected\u objs在调用后不会被更新。这是我第一次处理异步调用,这解释了我对理解的无知g如何在分页完成后更新全局变量。感谢@nicktomlin!我将阅读异步javascript,然后测试代码段。我将报告我的结果。