i have an issue with an AJAX call to give the response to the main function. Right now i use async false which i know is odd and i try to solve it but fail all the time.
This works (async false):
function printTable(){
var $table = $('#Table');
var view_data;
$.ajax({
type: "GET",
url: '/api/getdata',
success: function(response) {
var TableData = [];
for(var i =0;i < response.length ;i++){
var item = response[i];
var sessionID = item.sessionID;
if(sessionID){
$.ajax({
async: false,
type: "POST",
url: '/api/getNickname',
dataType: "json",
data: {
id: sessionID,
},
success: function(response){
view_data = response;
}
});
}
TableData.push({
_id: item._id,
datum: item.datum,
uhrzeit: item.uhrzeit,
session: view_data,
});
}
$table.bootstrapTable({data: TableData});
$table.bootstrapTable('togglePagination');
}
})
}
I tried the suggestion from here: Assigning ajax response to global variable without using async: false but it fails as view_data is undefined.
function printTable(){
var $table = $('#Table');
var view_data;
$.ajax({
type: "GET",
url: '/api/getdata',
success: function(response) {
var TableData = [];
for(var i =0;i < response.length ;i++){
var item = response[i];
var sessionID = item.sessionID;
if(sessionID){
$.ajax({
type: "POST",
url: '/api/getNickname',
dataType: "json",
data: {
id: sessionID,
}
}).done(function(response){
test(response)
});
function test(response){
view_data = response;
return view_data;
}
}
}
TableData.push({
_id: item._id,
datum: item.datum,
uhrzeit: item.uhrzeit,
session: view_data,
});
}
$table.bootstrapTable({data: TableData});
$table.bootstrapTable('togglePagination');
}
})
}
What i realized is if i put alert(view_data); before the push it works!? Im not that deep into JS/AJAX as is still learn it, so i hope someone might give me a hint or can help.. How can i pass var view_data to the main function without using async false?
Thanks...