sharif2008
sharif2008

Reputation: 2798

unable to get json response in php+codeigniter+jquery.ajax()

My view --

$(document).ready(function(){

    $(".varsity").change(function(){

       var id=$(this).val();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()?>/admin/get_varsity_faculty",
            data:"varsity_id="+id,
            dataType: 'json',
            success: function(data)
            {     
               alert(data);

            }
        });

    });

});

My controller(admin)

public function get_varsity_faculty()
{
   //admin controller
    $varsity_id=$this->input->post('varsity_id');
    $faculties=$this->admin_varsity->get_faculty_information($varsity_id);
    //print_r($faculties);
    echo json_encode($faculties);

}

when print_r($faculties) and alert response in my view (removing dataType:'json') get this output --

 Array
(
[0] => Array
    (
        [unit_id] => 1
        [vid] => 3
        [unit_name] => Faculty of Civil Engineering 
        [form_starting_date] => 2013-05-15
        [form_end_date] => 2013-05-22
        [admission_date] => 2013-05-22
        [possible_result_date] => 2013-05-22
        [class_start_date] => 2013-05-21
        [is_active] => 1
    )

[1] => Array
    (
        [unit_id] => 2
        [vid] => 3
        [unit_name] => Faculty of Electrical & Electronic Engineering 
        [form_starting_date] => 0000-00-00
        [form_end_date] => 0000-00-00
        [admission_date] => 0000-00-00
        [possible_result_date] => 0000-00-00
        [class_start_date] => 0000-00-00
        [is_active] => 1
    )

[2] => Array
    (
        [unit_id] => 12
        [vid] => 3
        [unit_name] => Civil Engg
        [form_starting_date] => 2013-06-11
        [form_end_date] => 2013-06-12
        [admission_date] => 2013-06-18
        [possible_result_date] => 2013-06-04
        [class_start_date] => 2013-06-10
        [is_active] => 1
    )

)

My model works really fine. BUT when I echo json_encode($faculties); with dataType:'json' in my view i am getting response like [object][object].And cant parse the response.I know its not a tough question.but i have tried so many times to overcome this myself.In a word my question is how can i get the json response and parse it form this array in my view. Thanks

Upvotes: 0

Views: 2965

Answers (2)

BIOS
BIOS

Reputation: 1695

It's showing [Object, Object] because the array you are encoding is an array of arrays which ends up as an array of objects in the eventual parsed json.

Ex:

<?php

    $array = array(
      0 => array('id' => 1),
      1 => array('id' => 2)
    );

    $json = json_encode($array);

The above will get encoded as:

[{"id" : 1}, {"id" : 2}]

Which when parsed with something like $.parseJSON will end up as an array with two objects or when we log it to the console:

[Object, Object]

or [object Object],[object Object] if you used alert instead of console.log (you shouldn't do that). Remember that alert only ever displays a string. If you want to be able to see the composition of the object returned you should always use console.log

You can access the individual objects like you would normally in a javascript array lookup. If the above is in a variable called json_result then you would use:

json_result[0] // this would return the first object of the array.

You can then use the objects key to retrieve the value:

json_result[0]['id'] // this will equal 1

Upvotes: 2

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

Try this

<?php
    public function get_varsity_faculty()
    {
        $varsity_id=$this->input->post('varsity_id');
        $faculties=$this->admin_varsity->get_faculty_information($varsity_id);

        $rows = array();
        foreach($faculties->result() as $row)
        {
            $rows[] = $row;
        }

        print json_encode($rows);
    }
?>

Upvotes: 0

Related Questions