2

I am trying to alert() the data returned by $.get or more accurately assign it to var data

var data = [];
function outside(text) {
data.push(text);
}

$.get( "../../services/content/test.php", function( content ) {
 outside(content);

});
alert(data); 
Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
Mustafa
  • 63
  • 6
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Barmar Sep 19 '13 at 08:55

4 Answers4

2

Your alert is called BEFORE you get result from Ajax request. If you do your alert in $.get callback, there will be an output available, so:

$.get( "../../services/content/test.php", function( content ) {
 outside(content);
 alert(data);
});

That's a normal behaviour of asynchronous requests.

hsz
  • 148,279
  • 62
  • 259
  • 315
  • Thanks but I want to use the data returned outside the function – Mustafa Sep 19 '13 at 08:59
  • So you have to call some function to handle returned data from your Ajax callback. There is no other reasonable way to do it. – hsz Sep 19 '13 at 09:01
  • Well, you could go for $.ajax({ url: "../../services/content/test.php", success: outside, async: false }); async calls are not preferable though. See http://api.jquery.com/jQuery.ajax – R. Oosterholt Sep 19 '13 at 09:03
0

Ajax calls are asynchronous so there is no guarantee that data contains a value when you alert it. Try alerting data in the callback.

var data = [];
function outside(text) {
data.push(text);
}

$.get( "../../services/content/test.php", function( content ) {
 outside(content);
 alert(data); 
});

Currently the code executes as follows:

var data = [];
function outside(text) {
    data.push(text); //4. result is pushed to array.
}

//1.  Ajax call is made
$.get( "../../services/content/test.php", function( content ) {
 outside(content); //3.  Ajax call returns and outside function is called
});
//2.  data is alerted
alert(data);

You can see that the array is alerted prior to the Ajax response being pushed on to the array.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Try this,

 var data = [];
 function outside(text) {
  data.push(text);
  alert(data); 
 }

 $.get( "../../services/content/test.php", function( content ) {
  outside(content);

 });

$.get is async means the browser wont wait for it to complete to execute next line

Manu M
  • 1,074
  • 5
  • 17
0

By default jQuery performs an asynchronous call to the server and then sends the result to the callback function you pass as an argument. This causes your alert to fire before your server has responded to the request and your content has been pushed into the data variable.

One more thing to think of is that you make an uneccessary wrapper function for your outside function. If outside is not a general function but specific for use only in that way there is no need for wrapping it - simply pass it as the second argument.

var data = [];
function outside(text) {
  data.push(text);
  alert(data);
}
$.get( "../../services/content/test.php", outside);