我坚持通过Ajax从数据库中获取结果。 id="view_errorcode"正在工作,但id="view_vlokasi"不是,只是显示未定义。谁能说出我在代码中做错了什么?
$('#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"}]
看起来像是对象数组,因此在不会循环数组时无法访问 lokasi 属性。试试这个:
var lokasi;
for(var i = 0; i < data.length; i++) {
lokasi = data[i].lokasi;
...
或者您可以从项目中获取特定元素,例如
data[0].lokasi
或
data[n].lokasi