当我单击搜索按钮并查看firebug中的网络选项卡时,它显示正在返回JSON对象,但我无法将它们加载到我的浏览器中进行查看。如果我把" var url"的部分拼凑在一起进入地址栏它也有效。我认为错误可能与最后一个函数有关,但我无法弄明白。
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
img {
height: 100px;
float: left;
}
#images{
width: 100%;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<input type="text" id="flickrInput">
<button id="flickrSearch">Search Photos</button>
<div id="images"></div>
<script>
$(document).ready(function () {
$("#flickrSearch").click(function (event) {
var searchVal = $("#flickrInput").val();
var flickrAPI = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=dd4a16666bdf3c2180b43bec8dd1534a";
$.getJSON( flickrAPI, {
tags: searchVal,
per_page: 25,
format: "json"
},
function( data ) {
$.each( data.items, function( i, item ) {
var url = 'https://farm' + item.farm + '.staticflickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';
$('#images').append('<img src="' + url + '"/>');
});
});
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
似乎Flickr API已经更改,您的示例已过时。
您只需要使用Google Chrome开发者控制台或使用断点和手表来调查来自API的数据。
有一个工作片段:
$(document).ready(function () {
$("#flickrSearch").click(function (event) {
var searchVal = $("#flickrInput").val();
var flickrAPI = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=dd4a16666bdf3c2180b43bec8dd1534a&nojsoncallback=1";
$.getJSON( flickrAPI, {
tags: searchVal,
per_page: 25,
format: "json"
},
function( data ) {
$.each( data.photos.photo, function( i, item ) {
var url = 'https://farm' + item.farm + '.staticflickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';
$('#images').append('<img src="' + url + '"/>');
});
});
});
});
img {
height: 100px;
float: left;
}
#images{
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="flickrInput">
<button id="flickrSearch">Search Photos</button>
<div id="images"></div>