codeigniter access data array in ajax success function


codeigniter access data array in ajax success function

我的问题是我无法访问从控制器返回给 Ajax 成功函数的数组。

这是我的控制器代码:

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;
}

函数显示未定义

根据你最新的评论,你的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); 
                });
            }
   });