2017-04-20 56 views
1

我有一个调用另一个函数的参数发送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值。

所以,我有两个问题:

  1. 如何将这个值返回给调用函数
  2. 直到HTTP POST已接收到响应如何停止调用函数的执行?
+1

也许异步/等待可以帮助你在这里。但是,如果没有,那么这是不可能的(停止执行调用者函数),你必须诉诸承诺或回调。 –

回答

1

如果您需要阻止执行,直到AJAX的回报,你可以在阿贾克斯参数指定async:false,按jQuery documentation

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

}