0

Here I have one requirement with AJAX and I gave one url(UpdateURL) to the url param. In success case I'm going to call one dataTable(called loadGrid()), here I need to call same url(UpdateURL) to the loadGrid(), meanwhile I'm calling two times same url(UpdateURL), it causes duplicate requests. Can any one help me how to use url(UpdateURL) single time and avoiding duplicate requests. Sorry for the confusion. Here is my code,

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

    });
}
2
  • What is this code doing? ie what is supposed to happen? Commented Feb 23, 2015 at 10:59
  • Why not save the response data in a global variable and make it available to both functions? Commented Feb 23, 2015 at 11:29

1 Answer 1

1

Can't understand why you need to call a ajax and a getJson for the same thing, try this:

$("#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();
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.