2012-03-01 75 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(); 
     } 
    }); 
}