I have a function with it i want to get value from a php file:
var phrases = null;
function setPhrases(lang) {
$.getJSON(locationHostname()+'json?json=lang&lang='+lang,
function(json) {
phrases = json;
alert( 1 + ' ' + phrases);//phrases = [object Object]
});
alert( 2 + ' ' + phrases);//phrases = null
}
setPhrases('en');
alert(3+' '+phrases);//phrases = null
how to set correctly it, that alert(3+’ ‘+phrases); gets an object instead of null? I want to use the function that e.g returns the value from getJSOn.
Thank you
$.getJSONis asynchronous , that’s why you get undefined: thealertsare executed while ajax call is still runningYou could call a function on success event, passing the
jsonas argument, e.g.