Jquery JSON Flickr API 返回一组照片
我试图从 Flickr 获取指定的图像集,然后使用 JSON 和 REST API 显示该集中的图像。这是我正在使用的代码:
$.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 密钥。当在我的网络浏览器中输入带有我的密钥的网址时,会返回以下错误:
jsonFlickrApi({"stat":"fail", "code":1, "message":"Photoset not found"})
我知道设置的 ID 是正确的,导航到 http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/ 工作正常。
更新
我设法让事情正常运转。这是我修改后的代码,以防其他人试图弄清楚。
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>"))
});
});
};
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");
});
});
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"})
I know the set id is correct, as navigating to http://www.flickr.com/photos/23892838@N07/sets/72157623858739780/ works fine.
UPDATE
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>"))
});
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 flickr API 文档 看来您的
集URL 中的
参数应为photoset_id
。From the flickr API docs it looks like your
set
parameter in your URL should bephotoset_id
.