admin管理员组

文章数量:1312634

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

    }

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

    }
Share Improve this question edited Oct 29, 2016 at 16:47 Jongware 22.5k8 gold badges55 silver badges102 bronze badges asked Oct 27, 2016 at 6:34 Nishan SinghaNishan Singha 1691 gold badge4 silver badges13 bronze badges 3
  • Parse the response to JSON manually using JSON.parse(str). OR use dataType: 'json' in ajax options. – Tushar Commented Oct 27, 2016 at 6:35
  • obj[0].section_id – madalinivascu Commented Oct 27, 2016 at 6:35
  • Your response is an array. You have to select the index you need or iterate each element – Weedoze Commented Oct 27, 2016 at 6:37
Add a ment  | 

5 Answers 5

Reset to default 4

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

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.

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

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.

本文标签: javascriptExtract json data from ajax responseStack Overflow