0
$.ajax({
        url: "",
        data: "",
        dataType: 'json',
        success: function (data) {
            var tblBody = '';
            for(var i=0;i<data.length;i++){
                $.ajax({
                    type: "GET",
                    url: "",
                    data: {},
                    success: function(response){
                        // creating table rows
                        tblBody += rowData;
                    },
                    error: function(){
                    }
                });
            }
            $("#losssummary").append(tblBody); // appending table all rows
            createDataTable('sample_1', "M d, yyyy");
        },
        error: function(err)
        {
        }
    });
});
rrk
  • 15,677
  • 4
  • 29
  • 45
Ajith
  • 258
  • 1
  • 7

3 Answers3

1

Try this code snippets.

Here I have used $.when().then()and then replaced the if with while.

$.ajax({
        url: "",
        data: "",
        dataType: 'json',
        success: function(data) {
            var tblBody = '',
              i = 0;
            while (i < data.length) {
                $.when(
                  $.ajax({
                      type: "GET",
                      url: "",
                      data: {},
                      success: function(response) {
                          // creating table rows
                          tblBody += rowData;
                      },
                      error: function() {}
                  })
              ).then(function(data, textStatus, jqXHR) {
                  i++;
              });
            }
            $("#losssummary").append(tblBody); // appending table all rows
            createDataTable('sample_1', "M d, yyyy");
        },
        error: function(err) {}
    });
John R
  • 2,741
  • 2
  • 13
  • 32
1
$.ajax({
        url: "",
        data: "",
        dataType: 'json',
        success: function (data) {
            var tblBody = '';
            var i = 0;
            selfCalling(data);

            function selfCalling(data){
                $.ajax({
                    type: "GET",
                    url: "",
                    data: {},
                    success: function(response){
                        // creating table rows
                        tblBody += rowData;

                        while(i<data.length){
                              selfCalling(data);
                              i++;
                        }
                    },
                    error: function(){
                    }
                });


            }
            $("#losssummary").append(tblBody); // appending table all rows
            createDataTable('sample_1', "M d, yyyy");
        },
        error: function(err)
        {
        }
    });
});

This uses recursive function to solve your problem. Hope it helps

1

Use complete option rather success option

Wahid Masud
  • 993
  • 10
  • 35