我一直坚持通过Ajax从数据库获取结果。 id =“ view_errorcode”正在工作,但id =“ view_vlokasi”不起作用,只是显示UNDEFINED。有人可以告诉我我在代码中做错了什么吗?
$('#showdata').on('click', '.item-info', function(){
var tid = $(this).attr('data');
$.ajax({
type: 'ajax',
method: 'get',
url: '<?php echo base_url() ?>RepeatProblem/infoReprob',
data: {tid:tid},
async: false,
dataType: 'json',
success: function(data){
var vlokasi = '<span style="font-size:22px;color:green">'+data.lokasi+'</span>';
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<p style="font-size:18px;color:black"><i class="fa fa-check"></i> '+data[i].lokasi+' '+data[i].updated_at+'<br>'+data[i].ketprob+'</p>';
}
$('#infoModal').modal('show');
$('#view_lokasi').html(vlokasi); // showing UNDEFINED
$('#view_errorcode').html(html); // Working
},
error: function(){
alert('Gagal Info Kode Error!');
}
});
});
ajax调用返回以下json
[{"tid":"550250","ketprob":"1","updated_at":"15-09-2018 19:39","lokasi":"ERA MART TENGKAWANG"},{"tid":"550250","ketprob":"2","updated_at":"15-09-2018 19:39","lokasi":"ERA MART TENGKAWANG"},{"tid":"550250","ketprob":"3","updated_at":"15-09-2018 19:39","lokasi":"ERA MART TENGKAWANG"},{"tid":"550250","ketprob":"4","updated_at":"15-09-2018 19:37","lokasi":"ERA MART TENGKAWANG"}]
答案 0 :(得分:2)
看起来像是对象数组,因此您无法访问lokasi属性,而不会循环数组。试试这个:
var lokasi;
for(var i = 0; i < data.length; i++) {
lokasi = data[i].lokasi;
...
或者您可以从类似项中获得特定元素
data[0].lokasi
或
data[n].lokasi