简体   繁体   中英

get value from json callback

I am not trying to append flickr photos to my webpage. I am trying to retrieve the latitude and longitude values from the json call back.

This is the code I have so far. Nothing happens when I load it into my browser.

<!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>

This

http://api.flickr.com/services/rest/&method=flickr.photos.search&api_key=

Should be

http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=

But the respond you will get is

<rsp stat="fail"><err code="3" msg="Parameterless searches have been disabled. Please use flickr.photos.getRecent instead."/>

Which is telling you to use flickr.photos.getRecent

I got it working. One of the problems is that I had jsoncallback=? which was returning a jsonp file with a syntax error, it was not returning json. I found the error on the jsonlint.com website. The error had jsonFlickrApi({ at the beginning of the file. I added &nojsoncallback=1 after &format=json and everything worked fine. I also declared my geoData variable before assigning values.

Here is the code:

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM