2015-02-23 37 views
0

在这里我有一个AJAX要求,我给了一个url(UpdateURL)url param.在成功的情况下,我要在这里呼吁一个dataTable(called loadGrid()), 我需要同url(UpdateURL)打电话到loadGrid(),同时我打了两次相同的url(UpdateURL),它会导致重复的请求。 任何人都可以帮助我如何使用url(UpdateURL)单次和避免重复的请求。对困惑感到抱歉。 这里是我的代码,如何避免重复使用性诉求jQuery的AJAX

$("#UploadButton").click(function(){ 
$("#idLoading").show();  
      var UpdateURL="some url"; 
      $.ajax({ 
      type: "post", 
      url: UpdateURL,  // calling first time 
      dataType: "json", 
      cache : false, 
      success: function(response) {   
      loadGrid(UpdateURL); // calling second time 
      $("#idLoading").hide(); 
      }, 
      error: function (xhr, status) { 
        $("#idLoading").show(); 
        timeout_trigger(); 
      }  
     }); 
    }); 

function loadGrid(url){ 
    $.getJSON(url,function (output) 
    { 
     try 
     { 
      var pdlJsonObj = output.aaData;    
        var tdata = $("#IdDatatble").dataTable({ 
         "aaData": pdlJsonObj, 
         "bDestroy": true, 
         "sPaginationType": "full_numbers",          
         "aaSorting": [],          
      "fnCreatedRow": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) 
          {       
          $(nRow).attr('id', iDisplayIndex); 
          }, 
      "fnInitComplete": function() 
          {       
          $("#IdDatatble tbody tr:eq(0)").find('td').each(function() { }); 
        } 
      }); 
     } 

     catch(err) 
     { 
      alert(err.message); 
     }  

    }); 
} 
+0

这是什么代码在做什么?即应该发生什么? – markpsmith 2015-02-23 10:59:59

+0

为什么不将响应数据保存在全局变量中并使其可用于这两个函数? – sideroxylon 2015-02-23 11:29:14

回答

1

不明白为什么你需要调用同样的事情一个AJAX和的getJSON,试试这个:

$("#UploadButton").click(function(){ 
    var UpdateURL="some url"; 
    $("#idLoading").show();  
    loadGrid(UpdateURL); 
}); 

function loadGrid(url){ 
    $.getJSON(url,function (output) 
    { 
     try 
     { 
      var pdlJsonObj = output.aaData;    
        var tdata = $("#IdDatatble").dataTable({ 
         "aaData": pdlJsonObj, 
         "bDestroy": true, 
         "sPaginationType": "full_numbers",          
         "aaSorting": [],          
      "fnCreatedRow": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) 
          {       
          $(nRow).attr('id', iDisplayIndex); 
          }, 
      "fnInitComplete": function() 
          {       
          $("#IdDatatble tbody tr:eq(0)").find('td').each(function() { }); 
        } 
      }); 
     } 

     catch(err) 
     { 
      alert(err.message); 
     }  

    }).done(function(json) { 
     $("#idLoading").hide(); 
    }) 
    .fail(function(jqxhr, textStatus, error) { 
     $("#idLoading").show(); 
     timeout_trigger(); 
    }); 
} 
+0

感谢您的好回答! – Rajasekhar 2015-02-24 05:48:06