I'm struggling to get access to array data outside of my ajax request.
This is the code I've got:
test.php contains:
echo json_encode($staffID);
JQuery is :
var id = [];
$.ajax({
type: "POST", cache: false, dataType: 'json',
url: "test.php",
success: function(data) {
id = data;
console.log (id) //SHOWS ARRAY VALUES
}
});
console.log (id) //DOESN'T SHOW ANYTHING
The console log output within the ajax request shows:
["1234", "2468", "3579", "0864"]
My aim is to run a couple of ajax requests and return a total of two different arrays. Once I can access the array data outside of the ajax result I will be able to check if other values are in the returned array data.
How do I access the array id
outside of the ajax request ?
EDIT: I'm trying to use the following :
function foo(callback) {
$.ajax({
url: '...',
success: function(response) {
return callback(null, response);
}
});
}
var result = foo(function(err, result){
if (!err)
console.log(result);
});
console.log(result);
does return the correct results, but is there any way I can access that as an array elsewhere in the script ?
Thanks