2017-08-22 43 views
0

因此,在我的程序中,我有两个选项卡,我正在执行一个使用表单的post ajax请求。 Ajax的两种不同的JSON的负荷将出现 代码如下:执行多个Ajax请求后调用函数

$('#myForm').submit(function() { 


$.ajax({ // create an AJAX call... 
     data : $(this).serialize(), // get formdata 
     dataType : "json", 
     //async : false, 
     //timeout : 55000, 
     type : $(this).attr('method'), // GET or 
     // POST 
     url : url.A, // the file to call 
     success : function (json) { 
      // on success.. 
      $('.ajaxloader').css("visibility", "hidden"); 
      Graph = json; 
      draw(Graph); 
     } 
    }); 


$.ajax({ // create an AJAX call... 
      data : $(this).serialize(), // get formdata 
      dataType : "json", 
      type : $(this).attr('method'), //POST 
      url : url.B, // the file to call 
      success : function (json) { 
       // on success.. 
       table= json; 
       plot(table); 
      } 
     }); 
} 

现在在josn的到来我感到首先显示表,查看我的其他选项卡上,我写一个onclick事件这种方式..

$(init); 
    function init() { 

      $('body').on('click', 'a.all',function(){ 
       plot(table); 
      }) 
      .on('click', 'a.domain',function(){ 
      draw(Graph); 
      }); 
    } 

现在我的问题是,如果我的领域,JSON就仍然没有加载,因此抛出一个错误点击, 我如何解决这个..

到目前为止,我试图async:false(它工作正常,但它显示不推荐使用的警告) AjaxStop,运作良好第一次然后当切换标签它只显示没有反应 我也试过但因为我是新的Java脚本我不明白全部使用

+0

如果使用全局变量来通知ajax调用是否完成,那么怎么样呢? 点击功能可以检查全局变量的值。 像 ' $( '身体')上( '点击', 'a.all',函数(){ 如果(全局变量){ 图(表);}} )。 ' –

回答

1

您可以尝试使用像ajaxComplete()这样的Ajax全局事件。

var ajaxCounter = 0; 

$(document).ajaxComplete(function(){ 
    ajaxCounter++; 

    if(ajaxCounter == 2){ 
    $(init); 
    } 
}); 
+0

大!你可以将答案标记为[接受](https://stackoverflow.com/help/someone-answers)? –