I’m using jQuery $.getJSON(..) to get some json.
The passed value shows me “undefined”.
Is there a way to do that?
getTopMenuJson : function(currentPage) {
if(currentPage == null) currentPage = 1;
var retJSON;
$.getJSON(
requestURLs.topMenuJson,
{
'ITEM_PER_PAGE' : TopMenuHandler.ITEM_PER_PAGE,
'CURRENT_PAGE' : currentPage
},
function(JSON) {
//alert(JSON); **<--This gives me result (Object)**
retJSON = JSON;
}
);
**alert(retJSON); //<!-- This doesn't, but Undefined**
},
It doesn’t and it shouldn’t as
getJSONis internally doing an AJAX call, the firstAin AJAX stands forAsynchronous, it simply means that the script execution won’t wait until yoursuccessfunction is called.You could instead use
$.ajaxand pass inasync: falseto one of its options to make sure that your script waits for the ajax call to be finished, but be aware that doing that would freeze the browser / tab until your AJAX call is finished.http://api.jquery.com/jQuery.ajax/