I am trying to call the ajax returned outside its function, below is the code I tried but didn't work.
var names;
$.ajax({
type: 'POST',
url: 'ajax/get_upcoming.php'
}).success(function (data) {
names = data[1];
});
alert(names);
I am trying to call the ajax returned outside its function, below is the code I tried but didn't work.
var names;
$.ajax({
type: 'POST',
url: 'ajax/get_upcoming.php'
}).success(function (data) {
names = data[1];
});
alert(names);
Because success function is callback function and called when ajax request get the response , so instead try this
var names;
$.ajax({
type: 'POST',
url: 'ajax/get_upcoming.php',
success: performOperation
});
function performOperation(data)
{
// write your code here
names = data[1];
}
I think alert() executes before the response comes back from the server. put console.log() and remove the alert and see the order of executions.