$.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)
        {
        }
    });
});
            Asked
            
        
        
            Active
            
        
            Viewed 459 times
        
    0
            
            
        - 
                    Set async: false option in your ajax call – msvairam Feb 15 '16 at 06:48
- 
                    alright, so many edits to your question.Now what is your problem ? – Manoz Feb 15 '16 at 06:49
- 
                    you are doing something wrong I think. Rethink what you r actually doing – Mir Gulam Sarwar Feb 15 '16 at 06:51
- 
                    use async:false; http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax#answer-25402055 – Dinesh Patra Feb 15 '16 at 06:51
- 
                    Yes. I tried async: false, It's not a good solution. Is there other way? – Ajith Feb 15 '16 at 06:51
- 
                    @Manoz Thanks for the edit. My problem is for loop is not waiting for the inner ajax response. – Ajith Feb 15 '16 at 06:53
- 
                    @Ajithshetty, At the moment doing `async:false` is most appropriate for your scenario. You can also take use of `.done()` of ajax – Manoz Feb 15 '16 at 06:56
- 
                    @Ajithshetty Did you try with `$.when()`? – John R Feb 15 '16 at 06:59
3 Answers
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
 
    
    
        Abhay Aradhya
        
- 21
- 4
 
     
    