我有一个调用函数,它会调用另一个带参数的HTTP POST函数。现在我想让这个被调用的函数阻塞执行,直到它的HTTP POST成功完成。
以下是我的逻辑代码:
简单地说,我希望在检查HTTP POST响应后再执行其他操作
因此,我有两个问题:
以下是我的逻辑代码:
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);
}
});
}
简单地说,我希望在检查HTTP POST响应后再执行其他操作
other stuff
。我已经遇到了另一个问题:初始插入的值为false
。在HTTP POST响应中的success(data)中,它具有true
值。但是,在调用函数中,随后的console.log
的值为undefined
。因此,我有两个问题:
- 如何将此值返回给调用函数
- 如何停止调用函数的执行,直到接收到HTTP POST响应?