繁体   English   中英

通过jQuery在Instagram JSON feed中获取未定义的变量

[英]Getting undefined variable in Instagram JSON feed via jQuery

我正在尝试使用JSON和jQuery从Instagram获取提要,以从一个特定用户提取特定的标签图像-到目前为止,它已经非常疯狂,并且都是从头开始构建的..现在我被卡住了-我的循环不断说Undefined x

这是我的代码

// GET INSTAGRAM FEED 
// Get feed from Instagram based on Hashtag and filter by user
//

var username = 'jdsportsofficial';
var hashtag = 'crlifestyle';
var clientId = '5a79ddf3fa4147ffbea3fc0e38b22014';
var auth_token = ''; // not needed for most
var instaHTML;
var divId = '#instafeed';

jQuery.ajax({
    type: "GET",
    dataType: "jsonp",
    cache: false,
    url: "https://api.instagram.com/v1/tags/"+hashtag+"/media/recent?client_id="+clientId,
    success: function(x) {

    for (var i = 0; i < 25; i++) {
        // GET THE PICTURE
        // -- options are thumbnail and large - see object for more
        var instaPicture = x.data[i].images.thumbnail.url;
        if (x.data[i].user.username == username) {

            instaHTML += "<div class='CaroselSlideItem'><img src='" + instaPicture + "'/></div>";

        }
        // INSERT THE GALLERY
        jQuery(divId).html(instaHTML);



    }
}
}); 

您可以通过jQuery('body').html(instaHTML);

我的错误是

TypeError: x.data[i]var instaPicture = x.data[i].images.thumbnail.url;未定义行707 var instaPicture = x.data[i].images.thumbnail.url;

如果x.data结果少于25,则可能会出现错误。 假设x.data只有20个结果,则当循环达到i=20x.data[i]将是未定义的。

所以

jQuery.ajax({
    type: "GET",
    dataType: "jsonp",
    cache: false,
    url: "https://api.instagram.com/v1/tags/" + hashtag + "/media/recent?client_id=" + clientId,
    success: function (x) {
        for (var i = 0; i < x.data.length && i < 25; i++) {
            // GET THE PICTURE
            // -- options are thumbnail and large - see object for more
            var instaPicture = x.data[i].images.thumbnail.url;
            if (x.data[i].user.username == username) {
                instaHTML += "<div class='CaroselSlideItem'><img src='" + instaPicture + "'/></div>";
            }
            // INSERT THE GALLERY
            jQuery(divId).html(instaHTML);
        }
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM