2016-07-02 6 views
1

私はこの{"test1":"My first test","test2":"my second test"}jqueryのAJAXは、CodeIgniterの

のような文字列値は、私が二<div>に1 <div>test2test1を表示するように努めました複数のdivの値が使用していることを解析します。しかし、私は未定義になっています。

以下は私のjqueryスクリプトです。

function getListOfProduct(val) { 
    // alert(val) 
    // $("#search-box").val(val); 
    $("#suggesstion-box").hide(); 
    var parent = $(this); 
    $.ajax({ 
     type: "POST", 
     datatype: "json", 
     cache: false, 
     url: base_url+'subscription/subscription/ajax_list_product', 
     data:'prodid='+val, 
     async: false, 
     success:function(res) 
     { 
      $("#listproduct").html(res.test1); 
      $("#new").html(res.test2); 
     }, 
     error: function (XHR, status, response) { 
      //alert(data); 
      alert('fail'); 
     } 
    }); 
} 

誰でも教えてください。

ありがとうございます!あなたの方法で購読/購読/ ajax_list_productから

+0

私はあなたの応答が要素の配列で構成されていると思うし、結果をhtmlオブジェクトに追加する前に 'res [0] .test1' –

+0

を試してみると、応答は正しい形式、すなわち行$( "#listproduct")の前です。html(res.test1);行警告を追加する(JSON.stringify(res));過去のアラート結果はこちら –

+0

res [0] .test1と$( "#listproduct").html(res.test1);私のために働いていません。 – premi

答えて

0

あなたの代わりに文字列 元のJSONとして結果を取得することができます。

$output=array(); 
$output["test1"]=My first test; 
$output["test2"]=My sec test; 
echo json_encode($output); 

その後、$アヤックス成功関数の応答を取得

success:function(res) 
{ 
    res=JSON.pars 

    $("#listproduct").html(res.test1); 
    $("#new").html(res.test2); 
}, 
関連する問題