2016-05-27 35 views
0

我想通過ajax在我的表單文本框更改事件中調用get_child方法。我想在datalist中顯示結果。下面是我使用的代碼。如何從json響應中刪除html代碼

$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" 
}] 

this is the output

感謝你們

+0

我不清楚你在問什麼。 –

+0

顯示帶有html標籤的json的示例 –

+0

如何從json響應中獲取特定值。問題是我的jQuery返回數組結果的頭信息,並且響應類型是String。 – Dilee

回答

0
[ 
{ 
    "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,而是一組元素。

+0

看到我的輸出圖像 – Dilee

0
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. 
+0

thanx @sikander。但錯誤表明在控制檯中,該標頭是未定義的 – Dilee

+0

$ .ajax(「 」類型:「GET」, 數據類型: 「JSON」, 頭: 「的contentType:應用/ JSON;字符集= UTF-8」, 成功:功能(數據,狀態){ 的console.log(數據); }, 錯誤:功能( xhr,desc,err){ console.log(xhr); } }); 使用此ajax方法(定義標題) – Sikander