0

我正在构建一个相当简单的无限照片页面,它从 Flickr 集合中获取照片。我有以下代码:

$(window).load(function() {

var apiKey = 'XXX';
var userId = 'XXX';
var photoset_id = 'XXX';

$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=1000' + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function(data) {
    $.each(data.photoset.photo, function(i, FlickrPhoto){
        var basePhotoURL = 'http://farm' + data.photos.FlickrPhoto[i].farm + '.static.flickr.com/'
        + data.photos.FlickrPhoto[i].server + '/' + data.photos.FlickrPhoto[i].id + '_' + data.photos.FlickrPhoto[i].secret + ".jpg";            

        var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + FlickrPhoto.id + "/";
        $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))
    });
});
});

我收到以下错误,我似乎无法弄清楚原因。可能主要失败但没有看到它..

TypeError: data.photos is undefined
[Break On This Error]   

...asePhotoURL = 'http://farm' + data.photos.FlickrPhoto[i].farm + '.static.flickr....
4

1 回答 1

1

修改您的 JQuery $.each。您不需要使用i索引每张照片。该函数针对photo数组中的每个元素执行。flickrPhoto因此代表来自 的每个项目data.photoset.photo,因此您不需要在它前面加上data.photos.

$.each(data.photoset.photo, function(i, flickrPhoto){
        var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + ".jpg";            

        var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";
        $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))
});
于 2012-12-20T23:17:51.660 回答