2017-04-20 101 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); 
    } 
    }); 

}