javascript - accessing variables outside ajax -
i have following code:
var src, flickrimages = []; $.ajax({ type: "get", url: "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=bf771e95f2c259056de5c6364c0dbb62&text=" + xmltitle.replace(' ', '%20') + "&safe_search=1&per_page=5&format=json", datatype: "json", statuscode: { 404: function() { alert('page not found'); } }, success: function(data) { $.each(data.photos.photo, function(i,item){ src = "http://farm"+ item.farm +".static.flickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_s.jpg"; flickrimages[i] = '<img src="' + src + '">'; }); } }); // undefined returned here flickrimages map.setzoom(13); map.setcenter(new google.maps.latlng(xmllat,xmllng)); infowindow.setcontent('<strong>' + xmltitle + '</strong><br>' + xmlexcerpt + '<br><br>' + flickrimages.join('')); infowindow.open(map,this); i trying access flickrimages variable outside ajax able put inside infowindow google maps. unfortunately outside ajax returns undefined.
i tried moving flickr things ajax unfortunately loses of other information such xmltitle , xmlexcerpt.
any appreciated.
thanks in advance,
dave.
the reason why flickrimages undefined comment is, because call $.ajax asynchronous, means not block until request completes.
that's why there success function gets "called back" when underlying http request completes. so, need handle flickrimages variable success function, or alternatively, success function, pass flickrimages other function processing.
Comments
Post a Comment