jQuery JSON Flickr API返回集合中的照片 [英] Jquery JSON Flickr API Returning Photos in a Set

查看:110
本文介绍了jQuery JSON Flickr API返回集合中的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Flickr获取指定的集合,然后使用JSON和REST API在该集合中显示图像.这是我正在使用的代码:

I am trying to get a specified set from Flickr and then display the images in that set using the JSON and REST API. Here is the code I am using:

    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=xxx&set=72157623858739780&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images");
  });
});

我从示例中删除了api密钥.键入我的Web浏览器的URL中包含我的密钥的URL返回以下错误:

I removed the api key from the example. The url with my key in it, when typed into my web browser, returns the following error:

jsonFlickrApi({"stat":"fail", "code":1, "message":"Photoset not found"})

我知道设置的ID是正确的,因为导航到 http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/效果很好.

I know the set id is correct, as navigating to http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/ works fine.

更新

我设法使事情顺利进行.这是我修改后的代码,以防其他人试图弄清楚.

I managed to get things working. Here is my revised code in case others are trying to figure it out.

function FlickrPhotoSet(){



    //SET API CALL BASED ON INPUT
    var apiCall = "http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=THESET&per_page=10&page=1&api_key=XXX&jsoncallback=?";

    //PRINT API CALL (DEBUG)    
    $("<span>").html(apiCall+"<br>").appendTo("body");

    //SEND API CALL AND RETURN RESULTS TO A FUNCTION    
    $.getJSON(apiCall, function(data){

        //LOOP THROUGH DATA
        $.each(data.photoset.photo, function(i,photo){

        //LINK TO IMAGE SOURCE
        var img_src = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_" + "s.jpg";

        //LINK TO IMAGE PAGE (REQUIRED BY FLICKR TOS)
        var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + photo.id + "/";

        //PLACE IMAGE IN IMAGE TAG AND APPEND TO IMAGES DIV 
        $("<img/>").attr("src", img_src).appendTo("#image_block")

        //WRAP IN LINK
        .wrap(("<a href='" + a_href + "'></a>"))
      });

    });


};

推荐答案

从flickr API文档看来,URL中的set参数应该为photoset_id.

From the flickr API docs it looks like your set parameter in your URL should be photoset_id.

这篇关于jQuery JSON Flickr API返回集合中的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆