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
6
  • 3
    Welcome to the wonderful world of async! You can't do that. Commented Aug 3, 2012 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. Commented Aug 3, 2012 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. Commented Aug 3, 2012 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. Commented Aug 3, 2012 at 16:39
  • Are you trying to see if the $get has been executed by the second alert? Commented Aug 3, 2012 at 16:46

5 Answers 5

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

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).

7 Comments

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.
@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.
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.
@BryanRoss — I think that is an overly literal interpretation of the body of the question that ignores the title of the question.
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...
|
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.

Comments

0

If I understand you right why can't you

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


function dosomething(data) {}

Comments

-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));

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.