我不是想将flickr照片添加到我的网页上。我正在尝试从json回调中检索纬度和经度值。
这是我到目前为止的代码。将其加载到浏览器中时没有任何反应。
<!DOCTYPE html>
<html>
<head>
<title>Flickr Pie Chart</title>
<meta charset="utf-8">
<script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#driver").click(function(event){
var apiKey = '[YOUR API KEY]';
var url = 'http://api.flickr.com/services/rest/&method=flickr.photos.search&api_key=' + apiKey +'&per_page=500&tags=losangeles&has_geo=1&extras=geo,tags&format=json&jsoncallback=?';
$.getJSON(url, function(data){
//loop through the results with the following function
$.each(data.items, function(i,item){
var geoData +='latitude:' item.latitude + '' + '<br>';
geoData += '' + item.longitude;
$('#results').append(geoData);
});
});
});
});
</script>
</head>
<body>
<div id = "results"></div>
<input type="button" id="driver" value="Load Data" />
</body>
</html>
答案 0 :(得分:0)
此
http://api.flickr.com/services/rest/&method=flickr.photos.search&api_key=
应该是
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=
但你得到的回应是
<rsp stat="fail"><err code="3" msg="Parameterless searches have been disabled. Please use flickr.photos.getRecent instead."/>
告诉你使用flickr.photos.getRecent
答案 1 :(得分:0)
我得到了它的工作。其中一个问题是我有jsoncallback =?这是返回一个语法错误的jsonp文件,它没有返回json。我在jsonlint.com网站上发现了错误。错误有jsonFlickrApi({在文件的开头。我在&amp; format = json之后添加&amp; nojsoncallback = 1并且一切正常。我还在分配值之前声明了我的geoData变量。
以下是代码:
<!DOCTYPE html>
<html>
<head>
<title>Flickr Pie Chart</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
var latitude;
var longitude;
var geoData;
$(document).ready(function(){
var apiKey = '[YOUR API KEY]';
var url = 'http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key='+ apiKey + '&tags=losangeles&has_geo=1&extras=geo,tags&format=json&nojsoncallback=1';
$.getJSON(url, function(data){
//loop through the results with the following function
$.each(data.photos.photo, function(i,item){
geoData += 'latitude' + ':' + item.latitude + '' + '<br>';
geoData += 'longitude' + ':' + item.longitude;
$('#results').append(geoData);
});
});
});
</script>
</head>
<body>
<div id = "results"></div>
</body>
</html>