试图访问使用jQuery

试图访问使用jQuery

本文介绍了试图访问使用jQuery Instagram的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Instagram的API和我做AJAX请求在do-whil​​e循环,直到next_url为空。我想要这个code做的是通过连续的请求,直到它完成获取所有的追随者。这有什么不对这个code?

I'm trying to use the Instagram API and I'm making AJAX requests in a do-while loop until the next_url is null. All I want this code to do is to fetch all the followers by making continuous requests until it's done. What is wrong in this code?

当我删除do-whil​​e循环它不给我一个错误,但只要一个我用的是循环中的AJAX请求,它永远不会停止。显然, $ next_url 串并没有改变对新取的 next_url - 为什么?什么是错的?

When I remove the do-while loop it doesn't gives me an error, but as soon as a I use the AJAX request within a loop, it never stops. Clearly the $next_url string is not changing to the newly fetched next_url - why? What is wrong?

$(document).ready(function(e) {
        $('#fetch_followers').click(function(e) {
            var $next_url = 'https://api.instagram.com/v1/users/{user-id}/followed-by?access_token={access-token}&count=100';
            var $access_token = '{access-token}';
            var $is_busy = false;
            var $count = 0;
            do {
                while($is_busy) {}
                $.ajax({
                    method: "GET",
                    url: $next_url,
                    dataType: "jsonp",
                    jsonp : "callback",
                    jsonpCallback: "jsonpcallback",
                    success: function(data) {
                        $is_busy = true;
                        $.each(data.data, function(i, item) {
                            $("#log").val($("#log").val() + item.id + '\n');
                        });
                        $("#log").val($("#log").val() + data.pagination.next_url + '\n');
                        $next_url = data.pagination.next_url;
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        $is_busy = true;
                        //alert("Check you internet Connection");
                        $("#log").val($("#log").val() + 'Error\n');
                    },
                    complete: function() {
                        ++$count;
                        $is_busy = false;
                    }
                });
            } while($next_url !== '' || $count <= 50);
        });
    });

在我没有在我的逻辑,我补充说,可以打破 $计数变量do-whil​​e循环,因为do-whil​​e循环是无限运行。加入后,它仍然运行无限的,我也不知道为什么。

After I failed in my logic, I added the $count variable that can break the do-while loop, because the do-while loop was running infinitely. After adding it, it still runs infinitely, and I have no idea why.

推荐答案

具备的功能调用自己在阿贾克斯成功回调新的URL作为参数:

Have the function call itself in the ajax success callback with the new url as a parameter:

$(document).ready(function() {
    $('#fetch_followers').click(function() {
        var $access_token = '{access-token}';
        pollInstagram('https://api.instagram.com/v1/users/{user-id}/followed-by?access_token={access-token}&count=100');
    });
});

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');
            });
            $("#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 ) {
                pollInstagram(data.pagination.next_url, ++count);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            //alert("Check you internet Connection");
            $("#log").val($("#log").val() + 'Error\n');
        }
    });
}​

这篇关于试图访问使用jQuery Instagram的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 21:10