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&view=json&callback=MyFunc",function(data){
var searchterm=data[location];
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&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&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+"&tagmode=any&format=json&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"]
}
}
]
}