簡體   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