Trying to access Instagram API using jQuery

后端 未结 1 1815
长情又很酷
长情又很酷 2021-01-03 16:34

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

1条回答
  •  北海茫月
    2021-01-03 17:31

    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');
            }
        });
    }​
    

    0 讨论(0)
提交回复
热议问题