0

Why is the xyz variable not updating even after JSON is coming from the php page ?

var xyz = '';
$.get("filteredRestaurant.php", {dineTypeIdString: checked}, function(data) {
            var response = JSON.parse(data);
            $.each(response, function(index, value) {
                xyz += 'a '; // line 5
            });
        });
        xyz += 'b';
        $('.filteredRestBody').html(xyz);

Even after line 5, the xyz's value is b Anyone, please help me out

Thanks in advance

Aman Singh
  • 743
  • 1
  • 10
  • 20

2 Answers2

1

$.get is ajax call which is asynchronous, moment ajax call is in progress statement

xyz += 'b'; 
$('.filteredRestBody').html(xyz); 

gets executed, while till moment after ajax call completes, try makes ajax call synchronous

Sangram Chavan
  • 331
  • 1
  • 9
-1

Use

async: false

for ajax

 $.ajax({
        async: false,
        // ...
        success: function(data) {

        }
    });
manowar_manowar
  • 1,215
  • 2
  • 16
  • 32