2012-03-01 74 views
1
function dispatchVehicle(){ 
     if(!chickAll()){ 
      return false; 
     } 
     var action = window.url + '?&method=dispatchVehicles' 
       + "&conLclFlag=" + conLclFlag 
       + "&containerId=" + containerId 
       + "&vehicleNameFlag=1" 
       + "&t=" + (new Date()).getTime(); 

     jQuery.getJSON(action, $('#vehicleForm').serialize(), function(data) { 
      if(data.flag){ 
       if (window.confirm(data.message)) { 
        alert(1); 
        var action1 = window.url + '?&method=dispatchVehicles' 
         + "&conLclFlag=" + conLclFlag 
         + "&containerId=" + containerId 
         + "&vehicleNameFlag=2" 
         + "&t=" + (new Date()).getTime(); 
        alert(2); 
        jQuery.getJSON(action1, $('#vehicleForm').serialize(), function(data1) { 
        alert(3); 
        if(!data1.flag){ 
         alert(data1.message); 
        } 
        if (data1.succ) { 
         window.parent.location.reload(); 
         window.parent.ClosePop(); 
        } 
        }); 
       } 
      } 
      if (data.succ) { 
       window.parent.location.reload(); 
       window.parent.ClosePop(); 
      } 
     }); 
    } 

我需要通過jQuery的getJSon函數執行兩個ajax調用。

問題是,一旦執行其中一個調用並且data.flag爲true,則調用第二個getJSon函數(因爲我需要第一個調用的結果作爲第二個調用中的一個條件)。 但是當函數運行時,代碼「alert(3)」不能執行。

任何幫助將不勝感激。

+0

在築巢它,嘗試建立回調陣列代替。 – 2012-03-01 09:58:42

+0

你的動作1的字符串是什麼。你可以發佈它,frebug中的任何JavaScript錯誤 – 2012-03-01 10:00:14

回答

0

AFAIK,.getJSON()應該嵌套OK。

儘管說實話,我可以嘗試我的代碼版本,但我所做的一切都是爲了更好的效率重新排列事物,並且更加確保內部.getJSON()與外部相同,並具有所需的變化。嵌套和結構是相同的。

的javascript:

function dispatchVehicle(){ 
    if(!chickAll()){ 
     return false; 
    } 
    var action = window.url + '?' + $('#vehicleForm').serialize();//reusable string 
    var dataObj = {//reusable object 
     method: 'dispatchVehicles', 
     conLclFlag: conLclFlag, 
     containerId: containerId, 
     vehicleNameFlag: 1, 
     t: (new Date()).getTime() 
    }; 

    $.getJSON(action, dataObj, function(data) { 
     if(data.flag){ 
      if (window.confirm(data.message)) { 
       alert(1); 
       //now reuse action and dataObj with a couple of changed properties 
       dataObj.vehicleNameFlag = 2; 
       dataObj.t = (new Date()).getTime(); 
       alert(2); 
       $.getJSON(action, dataObj, function(data1) { 
        alert(3); 
        if(!data1.flag){ 
         alert(data1.message); 
        } 
        if (data1.succ) { 
         window.parent.ClosePop(); 
         window.parent.location.reload(); 
        } 
       }); 
      } 
     } 
     if (data.succ) { 
      window.parent.location.reload(); 
      window.parent.ClosePop(); 
     } 
    }); 
}