I have a caller function that invokes another function that send a HTTP POST with parameters. Now i want that this called function blocks execution until there is its "success" (so when its HTTP POST has been done).
This is my logical code:
var fingerprint = null;
var janus_session = null;
var inserted = "false";
$(document).ready(function() {
//stuff
fingerprint = FindFingerprint(jsep);
janus_session = janus.getSessionId();
inserted = SendSDPLine(fingerprint, janus_session);
console.log("**in MAIN: inserted= " + inserted);
//other stuff
}
function SendSDPLine(fingerprint, janus_session) {
var sdp = fingerprint;
// var url = "http://localhost:8484/Shine/AccountController";
var action_type = "InsertSDPLine";
var sessionid = janus_session;
$.ajax({
type: "POST",
url: url,
xhrFields: {
withCredentials: false
},
data: {
"action": action_type,
"sdpline": fingerprint,
"sessionid": sessionid
},
success: function(data) {
if (data == "INSERTED") {
inserted = "true";
console.log("in SENDSDPLINE: inserted= " + inserted);
}
return inserted;
// return checkFingerprint (fingerprint);
},
// vvv---- This is the new bit
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown);
}
});
}
In few words, i want that other stuff
has been executed after that HTTP POST response has been checked. I've already seen another problem: initially, inserted has false
value. In success(data) in HTTP POST response, it has true
value. But, in the caller function, in the following console.log
has undefined
value.
So, i have two question:
If you need to block the execution until AJAX returns, you can specify async:false
in ajax parameters, as per jQuery documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With