JavaScript:如何调用阻塞式HTTP POST请求

8
我有一个调用函数,它会调用另一个带参数的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
因此,我有两个问题:
  1. 如何将此值返回给调用函数
  2. 如何停止调用函数的执行,直到接收到HTTP POST响应?

1
也许使用async/await可以帮助你解决这个问题。但如果不行,那么就无法停止调用函数的执行,只能使用promises或callbacks来解决。 - Sergio Tulentsev
2个回答

2
如果您需要在AJAX返回时阻止执行,可以按照jQuery文档中的要求,在ajax参数中指定async:false。请注意保留HTML标签。

0

使用回调函数

var fingerprint = null;
var janus_session = null;
var inserted = "false";


$(document).ready(function() {

//stuff

fingerprint = FindFingerprint(jsep); 


janus_session = janus.getSessionId();

//Use callback funcion to access 
SendSDPLine(fingerprint,janus_session , function(error,inserted){

  if(error){
     console.log("**Error in : "+error);
  }

  console.log("**in MAIN: inserted= "+inserted);


});


//other stuff

}

function SendSDPLine(fingerprint,janus_session, callback){

  var sdp=fingerprint;
//    var url = "http://localhost:8484/Shine/AccountController";
var action_type = "InsertSDPLine";
var sessionid = janus_session;

$.ajax({
  type:    "POST",
  url:     url,
  async: false, 
  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 result
      return callback(null, inserted);
    }



      //        return checkFingerprint (fingerprint);

    },
    // vvv---- This is the new bit
    error:   function(jqXHR, textStatus, errorThrown) {
      console.log("Error, status = " + textStatus + ", " +
        "error thrown: " + errorThrown
        );

      //return error
      return callback(errorThrown, null);
    }
  });

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接