如何使用jQueryAJAX避免重复请求



这里我有一个AJAX需求,我给了url param.一个url(UpdateURL)。如果成功,我将调用一个dataTable(called loadGrid()),这里我需要调用相同的url(UpdateURL)loadGrid(),同时我调用两次相同的url(UpdateURL),这会导致重复请求。有人能帮助我如何使用url(UpdateURL)单次并避免重复请求吗。很抱歉造成混乱。这是我的代码,

$("#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);
        }       
    });
}

无法理解为什么需要为同一件事调用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();
    });
}

最新更新