views:

50

answers:

1

Hi,

My problem is manage the code which get the tag and use is as variable (var searchterm= ??????). With JSON I want first get the "location" tags with tagthe and show the relate photos from flickr.

<!DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
</head>
<body>
 <div id="images">

</div>
<script>
$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&amp;view=json&amp;callback=MyFunc",function(data){         
              var searchterm=data[location];
        });


$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&amp;tagmode=any&amp;format=json&amp;jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });</script>
</body>
</html> 
+1  A: 

The second $.getJson() request is running before the first is complete, and the variable you created in the first is out of scope of the second anyway.

Place the second $.getJson() request inside the callback for the first so that it doesn't run until the first is complete.

$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&amp;view=json&amp;callback=MyFunc",function(data){         
    var searchterm=data[location];

    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&amp;tagmode=any&amp;format=json&amp;jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });
});

EDIT:

Here's a version that uses $.ajax() for the first call. I specified jsonp directly, and got rid of the callback in the URL.

Check out the live example: http://jsfiddle.net/uGJYr/2/ (updated)

$.ajax({
    url:"http://tagthe.net/api/?url=http://www.knallgrau.at/en&amp;view=json",
    dataType:'jsonp',
    success:function(data){

            // You needed to dig deeper to get the location
         var searchterm=data.memes[0].dimensions.location[0];

         $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&amp;tagmode=any&amp;format=json&amp;jsoncallback=?",
          function(data){
              $.each(data.items, function(i,item){
                  $("<img/>").attr("src", item.media.m).appendTo("#images");
                  if ( i == 3 ) return false;
              });
          });
    }
});

EDIT:

I changed the var searchterm = line above, as the data object returned is much more complex that suggested in your original code.

You needed:

var searchterm=data.memes[0].dimensions.location[0];

...because the data returned from the 1st request looks like this:

{"memes":[
            {   "source":"http://www.knallgrau.at/en",
                "updated":"Tue Jun 08 19:29:51 CEST 2010",
                "dimensions":{  "topic":["content","knallgrau","overview","agency","nullItem","Deutsch","foundation","Company","management","English"],
                                "content-type":["text/html"],
                                "author":["vi knallgrau"],
                                "person":["dieter rappold","Dieter Rappold"],
                                "title":["Company - vi knallgrau"],
                                "location":["Austria","Vienna"],
                                "language":["english"],
                                "size":["5494"]
                            }
            }
        ]
}​
patrick dw
But in this case we can not see the picture that relate with "location" ("location":["Austria","Vienna"])
asilloo
@asilloo - I updated the code and the live example link. The data returned from the first request is much more complex than `data[location]`. Please see my edits, and try the example.
patrick dw