我的问题是我无法访问从 Controller 返回到 ajax 成功函数的数组。
这里是我的 Controller 代码:
function get_slot()
{
$fac_id = 1;
$date = $this->input->post('date');
$this->load->model('booking_model');
$result = $this->booking_model->get_slot_availablity($date, $fac_id );
$data['booking'] = $result['row'];
echo json_encode($data);
}
我的 ajax 函数:
$.ajax({
type : "Post",
url : "<?php echo base_url(); ?>index.php/index/get_slot",
data : "date="+date,
dataType : 'json',
success : function(result)
{
$.each(result, function(index, val) {
alert(val.slot_id);
});
}
});
我的模型函数:
public function get_slot_availablity($date, $fac_id){
$q = $this->db->select('*')
->from('booking')
->where('f_id', $fac_id)
->where('date', $date);
$res['row'] = $q->get()->result();
return $res;
}
函数显示undefined
最佳答案
根据您的最新评论,您的 JS 代码应如下所示:
$.ajax({
type : "Post",
url : "<?php echo base_url(); ?>index.php/index/get_slot",
data : "date="+date,
dataType : 'json',
success : function(result)
{
$.each(result.booking, function(index, val) {
alert(val.slot_id);
});
}
});
关于php - ajax成功函数中的codeigniter访问数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28039733/