简体   繁体   中英

How to remove html code from json response

I'm trying to call get_child method through ajax in my form textbox change event. I want to show results in datalist. below is the code i have used.

    $sql = "SELECT * FROM tbl_child Where `id_mother`=?";
    $results = $db->load_result($sql,array('M-00000001'));
    $child = array();
    foreach($results as $row){  
        $child[]=$row;
    }
    echo json_encode($child,JSON_PRETTY_PRINT);
    die;

my script is:

$('#mother_name').on('keyup', function(e){
     //e.preventDefault();       
    $.ajax({
        url:"<?php echo $this->to_url('get-child'); ?>",
        type:"GET",
        datatype : "json",
        contentType: "application/json; charset=utf-8",
        success: function(data, status){
            console.log(data);
            //$(data).each(function() {
            //  names = "<option value=\"" + this.id_child + "\">" + this.child_name + "</option>";
            //  $('#childname').append(names);
            //});

        },
        error: function(xhr, desc, err){
            console.log(xhr);
        }
    });
});

but when i calling, following output is displayed. it contain html tags with results. when i select particular data from the result it said 'undefined' how can i solve this problem pls help me. i'm new to json.

  • Menu
  • Menu2

[
{
    "id_child": "0000000001",
    "id_mother": "M-00000001",
    "child_name": "marli",
    "child_lname": "",
    "dob": "2015-05-09 00:00:00",
    "gender": "1",
    "birth_weight": "3100.00",
    "birth_height": "55.00",
    "head_Perimeter": "34.00",
    "reg_by": "O-00000001",
    "created_date": "2016-05-12 21:40:25",
    "10": "2016-05-12 21:40:25"
}]

this is the output

thank you guys

[
{
    "id_child": "0000000001",
    "id_mother": "M-00000001",
    "child_name": "marli",
    "2": "Kathirvelan",
    "child_lname": "",
    "dob": "2015-05-09 00:00:00",
    "gender": "1",
    "birth_weight": "3100.00",
    "birth_height": "55.00",
    "head_Perimeter": "34.00",
    "reg_by": "O-00000001",
    "created_date": "2016-05-12 21:40:25",
    "10": "2016-05-12 21:40:25"
}]

From my understanding there are no html in here rather it is a array of elements .

 data =   [
    {
        "id_child": "0000000001",
        "id_mother": "M-00000001",
        "child_name": "marli",
        "child_lname": "",
        "dob": "2015-05-09 00:00:00",
        "gender": "1",
        "birth_weight": "3100.00",
        "birth_height": "55.00",
        "head_Perimeter": "34.00",
        "reg_by": "O-00000001",
        "created_date": "2016-05-12 21:40:25",
        "10": "2016-05-12 21:40:25"
    }]

Where is the html tag in the above output? and if you want to read any object value

console.log(data[0].id_child);

because its an array object, you have to put index to read the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM