我有一个调用另一个函数的参数发送HTTP POST的调用者函数。现在我希望这个被称为功能块的执行,直到它有“成功”(所以当它的HTTP POST已完成)。Javascript:调用阻塞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响应中成功(数据),它具有true
值。但是,在来电功能中,以下console.log
具有undefined
值。
所以,我有两个问题:
- 如何将这个值返回给调用函数
- 直到HTTP POST已接收到响应如何停止调用函数的执行?
也许异步/等待可以帮助你在这里。但是,如果没有,那么这是不可能的(停止执行调用者函数),你必须诉诸承诺或回调。 –