繁体   English   中英

如何从json响应中删除html代码

[英]How to remove html code from json response

我正在尝试通过表单文本框更改事件中的ajax调用get_child方法。 我想在数据列表中显示结果。 以下是我使用的代码。

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

我的脚本是:

$('#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);
        }
    });
});

但是当我打电话时,显示以下输出。 它包含带有结果的html标记。 当我从结果中选择特定数据时,它说“未定义”,我该如何解决此问题,请帮我。 我是json的新手。

  • 菜单
  • 菜单2

[
{
    "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"
}]

这是输出

感谢大伙们

[
{
    "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"
}]

据我了解,这里没有html,而是一个元素数组。

 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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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