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

4 Answers 4

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.

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

3 Comments

Thanks but I want to use the data returned outside the function
So you have to call some function to handle returned data from your Ajax callback. There is no other reasonable way to do it.
Well, you could go for $.ajax({ url: "../../services/content/test.php", success: outside, async: false }); async calls are not preferable though. See api.jquery.com/jQuery.ajax
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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.