Rushabh Shah Rushabh Shah - 1 month ago 7
Ajax Question

not displaying alert view in AJAX?

AJAX CODE:

$.ajax({
type:"post",
url:"<?php echo base_url()?>index.php/myad/filter",
data:{checkbox:checked},
success:function(data){
data = $.parseJSON(data);
var list = '';
var cates = '';
var base_url = "<?php echo base_url();?>";
console.log(data);
for(var i = 0; i <= data.length; i++){
list =+ '<div class="col-sm-12"><div class="product-view row" style="border-bottom:1px solid #eee;margin-bottom:20px;padding:20px 0px 20px 0px;background:#f1f1f1"><div class="col-sm-3 col-md-3 col-lg-3"> <div class="large-image"> <img alt="#" src="'+base_url+'/uploads/'+data[i]['checkbox'][0]['Image']+'"><div class="image-title"><span class="icon-thumbs-up" onclick="thumb('+data[i]['UniqueID']+',this)" style="font-size:24px;"></span></div></div></div><div class="col-sm-6 col-md-6 col-lg-6"> <div class="product-label"><h4>'+data[i]['FullName']+', '+data[i]['Area']+'</h4><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-calendar"></span> '+data[i]['SaleDate']+'</h5><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-clock"></span> ';
var starttime = data[i]['StartTime'];
var endtime = data[i]['EndTime'];
var times='';
for(var r = 0; r < starttime.length; r++){
times+=''+starttime[r]+'-'+endtime[r]+' ';
}
list+=times+'</h5><div data-balloon-length="fit" data-balloon="'+data[i]['Address1']+'" data-balloon-pos="up" ><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-home"></span> '+data[i]['Address1']+'</h5></div><div data-balloon-length="fit" data-balloon="'+data[i]['description']+'" data-balloon-pos="up" ><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-file"></span> '+data[i]['description']+'</h5></div></div></div><div class="col-sm-3 col-md-3 col-lg-3"><div class="product-label"><h4>CATEGORY</h4>';
for(var j = 0;j < data[i]['checkbox'].length; j++){
cates+='<h5 style="font-size:14px">'+data[i]['checkbox'][j]['Product']+'</h5>';
}
list+=cates+'</div></div></div></div>';
alert("for loop is end");
};
alert('loop is end');
}
});
}


it displaying alert of a for loop is end.but not displaying alert of a loop is end how to resolve it? Thanks in advance and sorry for any grammatical mistakes.

Answer

it displaying alert of a for loop is end.but not displaying alert of a loop is end

It's because your data variable is used in bad way:

for(var i = 0; i <= data.length; i++){

This line should be:

for(var i = 0; i < data.length; i++){

If the length of your data is "data.length" you cannot get the element at position data.length.

Array elements lie into the interval: 0,1,2.... length -1

In your case, when you try to get:

var starttime = data[i]['StartTime'];

with i = data.length, you get undefined. This means, the next loop:

for(var r = 0; r  < starttime.length; r++){

will throw an exception:

.... Uncaught TypeError: Cannot read property 'length' of undefined