Javascript:调用阻止HTTP POST

时间:2017-04-20 08:36:50

标签: javascript jquery http

我有一个调用函数,它调用另一个发送带参数的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响应之前停止执行调用函数?

2 个答案:

答案 0 :(得分:1)

如果你需要在AJAX返回之前阻止执行,你可以按照jQuery documentation在ajax参数中指定async:false

答案 1 :(得分: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);
    }
  });

}