I have defined a JavaScript function
function a()
{
var data = 'some data';
$.getJSON(networkController,data,function(r) {
$.each(r, function(a)
{
alert(r[a].networkAccessToken);
});
});
}
It works fine but When I do it as:
function a()
{
var data = 'some data';
$.getJSON(networkController,data,function(r) {
$.each(r, function(a)
{
return r[a].networkAccessToken;
});
});
}
var c = a();
alert(c);
Alert is undefined…..
It’s important to realise that your AJAX call is non-blocking. This means that it returns before a response is received from the server. (The first “A” stands for “Asynchronous”!)
The most common way to handle this is to use a callback: