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.