Marco
Marco

Reputation: 75

codeigniter access data array in ajax success function

my problem is that i can't access the array returned from controller to ajax success function.

here my controller code:

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

my ajax function:

 $.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); 
                    });
                }
       });

my model function:

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

the function displays undefined

Upvotes: 4

Views: 2264

Answers (1)

Mysteryos
Mysteryos

Reputation: 5791

As per your latest comment, your JS code should be as follows:

$.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); 
                });
            }
   });

Upvotes: 2

Related Questions