0

I'm catching data from Database with Ajax. In the second Ajax request I try to put the variables from first Ajax request to the new DOM elements from second request but there the variables have same value from last array possition.

I have no idea why . unfortunately I didn't find the information about that.

a simple example:

    //Firt ajax request
$.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "connect/get-fistinformations.php",
            data: {
                folder_type:folder_type
            },
            dataType: "json",
                for(var first_key in json){
                    var info = json[first_key];
                    //If I put there some alert - alert('some_alert'); - the rest of the code works fine
                    //Catching Data with Json - working correctly
                    var folder_id = info[0];
                    var folder_name = info[1];
                    var folder_date = info[2];
                    folder_id;
                    //Second ajax request
                    $.ajax({
                        type: 'GET',
                        contentType: 'application/json; charset=utf-8',
                        url: 'connect/get-secondinformations.php',
                        data: {
                            folder_type:folder_type,
                            folder_id:folder_id
                        },
                        dataType: 'json',
                        success: function(json){
                            for(var s_key in json){
                                var second_info = json[s_key];
                                //Catching more data from other Data table - working correclty
                                var count = second_info[0];
                                var route = second_info[1];
                                var distance = second_info[2];
                                var price = second_info[3];                                                                                                             
                                var price_per_km = +(price) / (+(route) + +(distance));
                                //Without the alert from the firt ajax request
                                //There inside new elements I try to put variable from first Ajax catching (folder_id, folder_name,folder_date)
                                //But every time when I add new elemets to DOM, these three variables have same value - from last arrray position
                                //For example folder_id = '50' | folder_name = 'Some folder name' | folder_date = '2020-07-04'
                                //New elements for DOM
                                if(count>0){
                                    var full_line_folder =  '<div class="full-line-folder folder">'+
                                                        '<i class="some-line"></i>'+
                                                        '<span class="folder-name">'+folder_name+'</span>'+
                                                        '<span class="folder-comment">Utworzono: '+folder_date+'<br />Pozycje: '+count+'<br />Dystans: '+route+'km<br />Stawka: '+price_per_km+' €/km<br />Puste kilometry: '+distance+'km</span>'+
                                                        '<input type="radio" name="folderid" value="'+folder_id+'" style="display:none;" checked/>'+
                                                    '</div>';
                                    $(full_line_folder).appendTo(point);
                                }else{
                                    var empty_line_folder = '<div class="empty-line-folder folder">'+
                                                                '<i class="empty-line"></i>'+
                                                                '<span class="folder-name">'+folder_name+'</span>'+
                                                                '<span class="folder-comment">Utworzono:'+folder_date+'<br />Pozycje: 0<br />Dystans: 0km<br />Stawka: 0 €/km<br />Puste kilometry: 0km</span>'+
                                                                '<input type="radio" name="folderid" value="'+folder_id +'" style="display:none;" checked/>'+
                                                            '</div>';       
                                    $(empty_line_folder).appendTo(point);
                                }
                            }
                            
                        },
                        error: function(note){
                            alert('error');                         
                        }
                    });
                }
            },
            error: function(note){
                alert('error');
            }
        });
Charlie
  • 22,886
  • 11
  • 59
  • 90
Jacob
  • 11
  • 2
  • Your second ajax hit must be inside the "success" of the first ajax hit, to make it work synchronously. – Helper Jul 04 '20 at 06:27

1 Answers1

0

The problem is that your first loop proceeds to the next iteration before the data arrives for the ajax request it makes. Mostly, by the time your first ajax response arrives, the loop is already gone and any external variables you try access will have their last values.

There are many workarounds for this. I don't think discussion all that is the scope of this answer.

Your options:

  1. Use closures correctly.

  2. Serialize the requests by using async/await

Charlie
  • 22,886
  • 11
  • 59
  • 90