【问题标题】:Javascript: call a blocking HTTP POST [duplicate]Javascript:调用阻塞的 HTTP POST [重复]
【发布时间】:2017-09-16 18:44:41
【问题描述】:

我有一个调用函数,它调用另一个发送带有参数的 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。我已经看到了另一个问题:最初,inserted 具有 false 值。在 HTTP POST 响应中的成功(数据)中,它具有 true 值。但是,在调用者函数中,以下console.log 具有undefined 值。

所以,我有两个问题:

  1. 如何将此值返回给调用者函数
  2. 如何在收到 HTTP POST 响应之前停止调用函数的执行?

【问题讨论】:

  • 也许 async/await 可以在这里为您提供帮助。但如果不是,那么就不可能(停止调用函数的执行),你必须求助于使用承诺或回调。

标签: javascript jquery http


【解决方案1】:

如果您需要阻止执行直到 AJAX 返回,您可以在 ajax 参数中指定 async:false,如 jQuery documentation

【讨论】:

    【解决方案2】:

    使用回调函数

    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);
        }
      });
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2015-11-25
      • 2020-03-29
      相关资源
      最近更新 更多