0

I'm using the jQuery function $.getJSON to get some data from the server side and here is the code:

function (){
  $.getJSON("someURI",function(result){
    //this alert works fine
    alert(result.msg) 
  });

  // this prints the data as undefined
  alert(result.msg); 
}

how to change the scope of "result" returned from $.getJSON in order to make it global.. to make the second alert prints its value not to be undefined ??

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
Ahmed Waheed
  • 1,281
  • 5
  • 21
  • 39

4 Answers4

4

You can't. AJAX is asynchronous meaning that you can access the result variable only inside the success callback. You could make it global, but remember that the line immediately following the $.getJSON call will be called much before the success callback executes rendering it useless.

So no global variables, consume the results of an AJAX call inside the callback:

function () {
    $.getJSON('someURI', function(result){
        // only here you can hope to consume the results of an AJAX call
        alert(result.msg) 
    });
}

That's how AJAX works. The A in AJAX stands for asynchronous.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

This is an asynchronous call.. So you must wait till the server responds... In your case, the outer alert will be undefined as the server hasn't responded yet... Also, the scope of result is limited to callback function, it's inaccessible...

You can do it like this though:

var myJSON;
var serverResponded = 0;

function () {
    $.getJSON("someURI", function (result) {
        //this alert works fine
        myJSON = result.msg;
        serverResponded = 1;
    });
    setTimeout(function () {
        if (serverResponded) {
            alert(myJSON);
        }
    }, 500);
}
hippietrail
  • 15,848
  • 18
  • 99
  • 158
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
  • 1
    You're explanation is great, but I wouldn't advise the OP to check if the server has responded with `setTimeout`. I would try to teach them how to embrace callback functions. They should be able to do whatever they need to do from the callback. – lbstr Aug 18 '12 at 16:46
  • 1
    Agreed :) I replied that because sometimes, when we post a question on `stackoverflow` we need a quick solution to get things done ASAP. The way I learned these things is like this.. First get things up and running and then find out better ways to do it again... Thanks for your suggestion though :) – AdityaParab Aug 18 '12 at 17:07
  • I understand and appreciate your politeness. Lots of people here get quite defensive when you provide criticism! – lbstr Aug 18 '12 at 19:00
1

The $.getJSON call is a shortcut to an Ajax call, and “Ajax” stands for “Asynchronous JavaScript and XML”.

Let's suppose you add var globalResult and that you perform a globalResult = result; in your success function. So, when you do a call to globalResult outside the callback, globalResult won't be assigned yet. So, if you really have to process it outside, you can check if it's assigned with a function called setInterval, but this is going too far.

Jill-Jênn Vie
  • 1,849
  • 19
  • 22
0

save it into another variable something like this

function (){
var something;
  $.getJSON("someURI",function(result){
    //this alert works fine
    alert(result.msg) 
    something = result
  });

if you want it to be scoped outside your function wrapper, do something like this

var something;
function (){
  $.getJSON("someURI",function(result){
    //this alert works fine
    alert(result.msg) 
    something = result
  });
Chadit
  • 965
  • 2
  • 12
  • 27