Nishan Singha
Nishan Singha

Reputation: 169

Extract json data from ajax response

Here is my js function

<script>        
function get_class_group(class_id) {

        $.ajax({
            url: '<?php echo base_url();?>index.php?admin/get_class_group/' + class_id ,
            success: function(response)
            {
                var obj = response;
                alert(obj);
            }
        });

    }

</script>

This gives me output like

[{"section_id":"13","name":"A","nick_name":"A","class_id":"13","group_id":"4","teacher_id":"6","id":"4","group_name":"Science"}]
[{"section_id":"13","name":"A","nick_name":"A","class_id":"13","group_id":"4","teacher_id":"6","id":"4","group_name":"Science"},
{"section_id":"22","name":"B","nick_name":"b","class_id":"13","group_id":"4","teacher_id":"0","id":"4","group_name":"Science"}]

If i write alert(obj.section_id); it gives me

error:"undefined".

How can i get a specific value from this json?

Here is my backend function:

function get_class_group($class_id) 
    {
        $this->db->select('section.*, class_group.*');
        $this->db->from('section');
        $this->db->join('class_group', 'section.group_id = class_group.id', 'left');
        $this->db->where('section.class_id', $class_id);
        $groups = $this->db->get()->result_array();

        foreach ($groups as $row) {
            $value[] = $row;
            echo json_encode($value);
        }

    }

Upvotes: 2

Views: 12013

Answers (5)

darpan_systematix
darpan_systematix

Reputation: 72

The output/response seems in json array format. And to access any particular element from json array, you will need to specify the index position in array.

Please try below to check any particular element data:

alert(obj[0].section_id)

This should alert the 'section_id' of index 0 (i.e, very first element in array)

You can always change index to get data of specific index.

Upvotes: 0

Praveen Kumar T
Praveen Kumar T

Reputation: 84

obj is an array ,so you are getting alert(obj.section_id) as undefined.

use below code to get value

alert(obj[0].section_id); // you will get output as 13
alert(obj[1].section_id); // you will get output as 22

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 5246

As such in response you are getting multiple items in the json object you have to use $.each to get value for each item.

//each loop
$.each(obj,function(key,item){
    alert(item.section_id);
});

Please check working snippet below :

var obj =[{
	"section_id": "13",
	"name": "A",
	"nick_name": "A",
	"class_id": "13",
	"group_id": "4",
	"teacher_id": "6",
	"id": "4",
	"group_name": "Science"
}, {
	"section_id": "22",
	"name": "B",
	"nick_name": "b",
	"class_id": "13",
	"group_id": "4",
	"teacher_id": "0",
	"id": "4",
	"group_name": "Science"
}];

$.each(obj,function(i,v){
    console.log(v.section_id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

madalinivascu
madalinivascu

Reputation: 32354

Use a each loop, your response is a array of objects,use stringify to turn the string in a object

$.ajax({
   url: '<?php echo base_url();?>index.php?admin/get_class_group/' + class_id ,
   success: function(response) {
     response = JSON.stringify(response);
     $.each(response,function(i,v){
       console.log(v.section_id);
     });
   }
});

place the echo outside the foreach function

foreach ($groups as $row) {
            $value[] = $row;

        }
header('Content-Type: application/json');//add the json header if you want to remove the js stringify function 
echo json_encode($value);

Upvotes: 4

Piotr Janik
Piotr Janik

Reputation: 1

It looks like your response is pure string.

Ensure that backend is adding Content-Type: application/json header in the response.

OR:

Try var res = JSON.parse(response) so that string is converted to an object.

Upvotes: 0

Related Questions