4

Possible Duplicate:
How can I get jQuery to perform a synchronous, rather than asynchronous, AJAX request?
Get a variable after ajax done

The two alert functions in the following code returns different results. I am trying to get the second to evaluate true too.. Any help is appreciated.. Thanks..

var array;

  $.get('php/getstocklist.php', function(data){  

  array = data; 
  alert($.isArray(array));    //alerts true

  }, "json");

alert($.isArray(array));      //alerts false
Community
  • 1
  • 1
MegaByteMe
  • 91
  • 6
  • 3
    Welcome to the wonderful world of **async**! You can't do that. – SLaks Aug 03 '12 at 16:30
  • 1
    While technically correct, that comment doesn't really help. You should probably explain a bit more about why it doesn't work the way he expects it to. – rossipedia Aug 03 '12 at 16:34
  • Maybe there is another answer, but I don't understand what you are trying to accomplish. What is suppose to be alerting. How is one false and the other not. – Elliott Aug 03 '12 at 16:38
  • 1
    @Elliott — The one inside the callback function has `data` assigned to it. The one on the last line is executed before the HTTP response comes back so is still `undefined`. – Quentin Aug 03 '12 at 16:39
  • Are you trying to see if the $get has been executed by the second alert? – Elliott Aug 03 '12 at 16:46
  • i am trying to store the data in the data array into another array which I will then return using a function.. But it does not work.. I tried the timeOut() function, although it helped storing the value, did not help solve my problem.. – MegaByteMe Aug 03 '12 at 16:48

5 Answers5

1

The problem is that the $.get is async. The var is global as you have it now. It just isn't defined because your second alert runs immediately before the ajax has returned and run its success callback.

In order to use it outside the the callback you would need to poll a variable to make sure the result has been assigned... but then you are essentially using a fake callback so it makes more sense to restructure your code and do it the normal way :-)

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
0

Here's what's happening:

// This variable _is_ global. However, it's current value is undefined
var array;

$.get('php/getstocklist.php', function(data){  

  // After this runs, _now_ array has data in it
  array = data; 

  // thus causing $.isArray to return true here.
  alert($.isArray(array));    //alerts true

}, "json");

// This call happens BEFORE the callback to the $.get call,
// so as of this line, array is still undefined, which results in 
// the $.isArray call returning false.
alert($.isArray(array));      //alerts false

Javascript is a heavy asynchronous language. You have to kind of break away from procedure thinking, and instead think in terms of passing bits of code around to be executed later (those are callbacks).

rossipedia
  • 56,800
  • 10
  • 90
  • 93
  • You are still trying (on the last line) to alert the variable before it has had `data` assigned to it. The function that assigns the value doesn't run until the HTTP response arrives. – Quentin Aug 03 '12 at 16:36
  • @byteME — It is an *empty* array (assigned on line 1), not the value passed as `data`. You could remove the entire ajax call and it would give the same result. – Quentin Aug 03 '12 at 16:37
  • Don't read into it. He asked how to make the $.isArray call return true. And my answer was correct. If he runs into other problems because of a misunderstanding of async, he can ask another question. – rossipedia Aug 03 '12 at 16:38
  • @BryanRoss — I think that is an overly literal interpretation of the body of the question that ignores the title of the question. – Quentin Aug 03 '12 at 16:40
  • Sorry my bad, it does not actually work.. the window.array is an array but it does not store the values of the data array... – MegaByteMe Aug 03 '12 at 16:45
  • @Quentin Good point. I have edited my answer – rossipedia Aug 03 '12 at 16:45
  • @byteME Please see my updated answer for a more comprehensive explanation of what's going on. – rossipedia Aug 03 '12 at 16:46
0

The second alert executes before the first because the $.get callback executes asynchronously. If you want the second alert to return true, you should use a jQuery Deferred object like so:

var array;

deferred = $.get('php/getstocklist.php', function(data){  

  array = data; 
  alert($.isArray(array));    //alerts true

}, "json");

// alerts true if $.get succeeded
deferred.done(function () { alert($.isArray(array)); }); 

The callback you pass to done will fire when the asynchronous $.get call returns.

Maros
  • 1,825
  • 4
  • 25
  • 56
0

If I understand you right why can't you

$.getJSON('php/getstocklist.php', function(data){  
    dosomething(data);
});


function dosomething(data) {}
Elliott
  • 186
  • 1
  • 3
  • 10
-3

Its a bit off a fudge but try setting using that will break your variable out off the request and make it global

window.array = data;  

alert($.isArray(window.array));
Dominic Green
  • 10,142
  • 4
  • 30
  • 34
  • It is already a global (unless that code is in a function, in which case it is *still* in the same scope). The problem is that the alert is run before the HTTP response gets back. – Quentin Aug 03 '12 at 16:35