0

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

No problem. Glad I could help. Happy hacking :)
really awesome.hope i would give you more changes to be glad further ;)
plz one question :- dataType: 'json', what the functionality of this in Jquery.ajax().does it atomatically returns javascript object from json response or else?
Yep. It specifies the return data expected and will return a javascript object subject to strict parsing and on the condition that the json is not malformed. See the dataType section of the JQuery API for more info.
Ha thanks.I think it's a pretty good description of my capabilities :P
0

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

1 Comment

its effects nothing.thanx for your try :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.