javascript - getJSON returning data -



javascript - getJSON returning data -

i have looked through here , realize doesn't getjson returns object unusable. problem having trying edit else's code pull image flickr. trying create function homecoming description of pictures. know doesn't homecoming info there has way update global variable or somehow pass off info need variable homecoming function. jist of have far.

function add_description(n){ var img_id = string(n); var textinfo =""; $.getjson('http://api.flickr.com/services/rest/?method=flickr.photos.getinfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){ textinfo = string(data.photo.description._content); alert(textinfo); homecoming textinfo; }) }

this code tried after updates george. thanks!

var testobj=$.getjson('http://api.flickr.com/services/rest/?method=flickr.photos.getinfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){ textinfo = string(data.photo.description._content); alert(textinfo); homecoming textinfo; })

the first step assign results textinfo variable:

$.getjson(...) { textinfo = string(data.photo.description._content);

the $.getjson() phone call asynchronous, code return textinfo; runs before $.getjson() phone call completes. therefore, textinfo variable still empty string. need phone call other code within $.getjson() call, or delay execution of homecoming until asynchronous phone call completes.

this reply based on similar reply found here.

edit (based on question update):

you not successful including homecoming statement within scope of $.getjson(). can assign result variable textinfo have done, , value available when $.getjson completes.

however, must ensure phone call has completed before attempting accessing value. can utilize settimeout(), seems messy, or can substitue $.ajax() $.getjson() phone call can utilize 'async = false' option. forcefulness phone call finish before execution continues, allowing variable populated before returning it.

javascript json flickr getjson

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -