2016-05-27 5 views
0

フォームテキストボックスの変更イベントでajaxを通じてget_childメソッドを呼び出そうとしています。データリストに結果を表示したい。以下は私が使ったコードです。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({ url: "<?php echo $ this-> to_url( 'get-child');?>"、 タイプ: "GET"、 データ型"JSON"、 ヘッダー: "のcontentType:アプリケーション/ JSON;のcharset = UTF-8"、 成功:関数(データ、ステータス){ にconsole.log(データ); }、 エラー:関数( xhr、desc、err){ console.log(xhr); } }); このajaxメソッドを使用する(ヘッダーを定義する) – Sikander

関連する問題