codeigniter访问ajax成功函数中的数据数组

时间:2015-01-20 07:21:37

标签: php jquery arrays ajax codeigniter

我的问题是我无法访问从控制器返回到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;
}

该功能显示未定义

1 个答案:

答案 0 :(得分:2)

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