I'm puzzled by the behavior of an AJAX request. When I call the "done" function, I expect it to be done immediately. However, I only receive the values after using a setTimeout function. Why is this happening?
{"status":"1","msg":"Return Successful","callbackFunct":"LinkMenu.setMenuItems()","return":[{"name":"save","image":"","action":"","status":""},{"name":"back","image":"","action":"","status":""},{"name":"delete","image":"","action":"","status":""}]}
class ServerQuery {
constructor(request, level) {
console.log("Starting server query for " + request + " at level " + level );
this.lev = level;
this.file = "http://" + location.hostname + "/" + this.lev + "/" + request;
this.values = new Object();
this.data = new Array();
this.result = null;
this.setRequiredValues();
}
setRequiredValues() {
console.log("Setting required values");
let ses = document.getElementById("key").value;
let orgid = document.getElementById("OrgId").value;
let userid = document.getElementById("userid").value;
this.values['key'] = ses;
this.values['orgid'] = orgid;
this.values['userid'] = userid;
console.log("Loaded required values: " + JSON.stringify(this.values));
}
addValue(key, insert) {
console.log("Adding value " + key + ": " + JSON.stringify(insert));
this.r = new Object();
this.r[key] = insert;
this.data.push(this.r);
console.log("Values added: " + JSON.stringify(this.data));
}
// Use this method to trigger a return callback
sendRequest() {
console.log("Server Query sending Request");
connect_ajax();
// this.values is an object
this.values['linked'] = this.data;
let req = JSON.stringify(this.values);
let uandp = "requesting=" + req;
console.log("Data being added: " + uandp);
$.post(this.file, uandp)
.done(function done2(result) {
console.log("Server query finished with result: " + result);
this.r = JSON.parse(result);
if (this.r.status == 1) {
console.log("ServerQuery after parse: " + this.r);
console.log("Output: " + this.r.callbackFunct);
if (typeof this.r.callbackFunct != 'undefined') {
setTimeout(function() {
this.r.callbackFunct(this.r.callbackVars);
}, 500);
} else {
alert("Callback not set");
}
}
else if (this.r.status == 3) {
alert(this.r.msg);
}
})
.fail(function processFailed() {
console.log("An error has occurred");
})
.always(function processAlways() {
console.log("Finished");
});
console.log("Requesting URL: " + this.file);
}
// Use this method to get a static response from the server
callRequest() {
console.log("Starting serverquery process()");
let answer = this.process();
console.log("Process returned: " + answer);
return answer;
}
process() {
this.values['linked'] = this.data;
let req = JSON.stringify(this.values);
let uandp = "requesting=" + req;
let file = this.file;
console.log("Data adding " + uandp);
let return_first = function () {
let tmp = null;
$.ajax({
'async': false,
'type': "POST",
'global': false,
'dataType': 'html',
'url': file,
'data': uandp,
'success': function (data) {
tmp = data;
}
});
return tmp;
}();
return return_first;
}
cleanUp() {
delete this.file;
delete this.values
console.log("Removed values from global scope: " + this.values);
console.log("Removed file from global scope: " + this.file);
}
}