On condition calling Ajax, but it is returning last value.
Here is my code
if(f_ref==209){
array1=[1,2,3,4,5,6];
for(var i = 0; i < array1.length; i++) {
console.log("loop", array1[i], i, array1.length);
getvalue = array1[i];
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
dataType: 'json',
url: "/getReportData",
data: { getvalue:getvalue},
//cache: false,
success: function(result) {
console.log(result.length);
resultarr=result;
console.log(resultarr.length);
console.log(i);
if(i==0){
$("#1").val(resultarr.length);
console.log(resultarr.length);
}
else if(i==1){$("#2").val(resultarr.length);}
else if(i==2){$("#3").val(resultarr.length);}
else if(i==3){$("#4").val(resultarr.length);}
else if(i==4){$("#5").val(resultarr.length);}
else if(i==5){$("#6").val(resultarr.length);}
},
error: function(error) {
console.log(error);
}
});
}
}
On if condition true it goes inside, Assign array1 values. For condition is true, goes inside.
console.log("loop", array1[i], i, array1.length) Value here is 1, 1, 0, 6
Condition is true it should go inside Ajax and call data on success. But it is returning zero value.
On checking here "console.log(i)" It is giving output 6 for each loop.
Why is this happening, i have no clue.
whats wrong in code above. Why for 'i'=6 it is taking Ajax request, also for 'i' = 6 condition is false why it is gong inside