使用jquery在一个json请求中获取Facebook照片

时间:2013-07-05 07:05:28

标签: jquery html json facebook

好的,所以我在Facebook上有一个“页面”以及它自己的网站。我想将“页面”中的所有照片链接到网站,以便我可以编辑一个图库。我知道如何接收一张图片,但我不知道如何接收多张图像然后显示它,比方说,一个新的div?我用谷歌搜索了!@#$,但仍然没有提出任何事情......提前致谢:D

var fburl_photo = "http://graph.facebook.com/868.Rotary.Northstar.RCACS/albums?fields=photos";
$.getJSON(fburl_photo,function(data){
    var albums = data["picture"];
    $("#albums").append("<div>" + albums + "</div>");
});

1 个答案:

答案 0 :(得分:1)

这应该让你开始。如果有什么不清楚,请告诉我。

var url = 'http://graph.facebook.com/';
url += '868.Rotary.Northstar.RCACS/albums?fields=photos'; 
//to save some space here

$(function(){

    $.ajax({
        url: url,
        dataType: 'jsonp',
        success: function(data){

            $.each(data.data, function(k1, album){

                if(k1 > 1) //just showing the first 2lists for demo purpose
                    return true; //skipping the rest

                var pictureArray = album.photos.data;
                //get an array of photos                    

                $.each(pictureArray, function(k2, pictureObject){

                //pictureObject.picture contains the image url

                //create a new image tag and append it to the body

                    var $img = $('<img/>')
                                   .prop({ src: pictureObject.picture })
                                   .wrap('<a href="#anchor"></a>')
                                   .appendTo('body');

                });

            });

        }
    });

});

示例:http://jsfiddle.net/gVZuC/