2012-12-16 64 views
1

我有這樣的數據:不能讓jQuery的模板工作

[ 
{ 
    "id": "46", 
    "title": "test", 
    "description": "", 
    "date_created": "2012-12-16 15:31:51", 
    "date_expires": "2013-01-14 17:37:40", 
    "date_renewal": "2012-12-15 17:37:40", 
    "renewals": 0, 
    "price": "333", 
    "mrt": 0, 
    "location_id": 0, 
    "email": "", 
    "password": "", 
    "phone": "", 
    "default_image": "", 
    "property_type_id": 0, 
    "share_basis_id": 0, 
    "type": 0, 
    "views": 0, 
    "is_featured": 0, 
    "is_recommended": 0, 
    "has_furniture": 0, 
    "has_wifi": 0, 
    "has_airconditioner": 0, 
    "has_bills_included": 0, 
    "slug": "", 
    "is_deleted": 0, 
    "images": [], 
    "guid": "50cddb576d3cf", 
    "location_name": "Lim Chu Kang", 
    "property_type_name": "HDB", 
    "share_basis_name": "Shared room" 
}, 
{ 
    "id": "45", 
    "title": "ggghh", 
    "description": "", 
    "date_created": "2012-12-16 15:31:51", 
    "date_expires": "2013-01-14 16:49:26", 
    "date_renewal": "2012-12-15 16:49:26", 
    "renewals": 0, 
    "price": "0", 
    "mrt": 0, 
    "location_id": 0, 
    "email": "", 
    "password": "", 
    "phone": "", 
    "default_image": "", 
    "property_type_id": 0, 
    "share_basis_id": 0, 
    "type": 0, 
    "views": 0, 
    "is_featured": 0, 
    "is_recommended": 0, 
    "has_furniture": 0, 
    "has_wifi": 0, 
    "has_airconditioner": 0, 
    "has_bills_included": 0, 
    "slug": "", 
    "is_deleted": 0, 
    "images": [], 
    "guid": "50cddb576d474", 
    "location_name": null, 
    "property_type_name": null, 
    "share_basis_name": null 
}, 
{ 
    "id": "44", 
    "title": "ggg", 
    "description": "", 
    "date_created": "2012-12-16 15:31:51", 
    "date_expires": "2013-01-14 16:41:48", 
    "date_renewal": "2012-12-15 16:41:48", 
    "renewals": 0, 
    "price": "56701", 
    "mrt": 0, 
    "location_id": 0, 
    "email": "", 
    "password": "", 
    "phone": "", 
    "default_image": "", 
    "property_type_id": 0, 
    "share_basis_id": 0, 
    "type": 0, 
    "views": 0, 
    "is_featured": 0, 
    "is_recommended": 0, 
    "has_furniture": 0, 
    "has_wifi": 0, 
    "has_airconditioner": 0, 
    "has_bills_included": 0, 
    "slug": "", 
    "is_deleted": 0, 
    "images": [], 
    "guid": "50cddb576d517", 
    "location_name": "Jurong East", 
    "property_type_name": null, 
    "share_basis_name": null 
} 

]

,我有這個模板:

<script id="tmp" type="text/x-jquery-tmpl"> 
     <b>${title}</b> 
     <b>${id}</b> 
     <br /> 
</script> 

和我的jQuery代碼是這樣的:

$.post('map/search-submit/', { 
         data: $('#map_form').serialize() 
        }, 
         function(data) 
         { 
          console.log(data); 

          $("#tmp").tmpl(data).appendTo("#results"); 
         } 
      ); 

其中data是我寫的ab的json數據OVE。

我得到的只是空白的html標籤,沒有數據。

<div id="results"> 
    <b></b><b></b><br /> 
</div> 

我在做什麼錯?

+0

你可以發佈一些東西給jsfiddle.net,演示你遇到的問題? – ditkin

回答

0

我發現了什麼的問題是:

返回的數據被視爲字符串(我是用PHP的json_encode($數據)發送它;)

所以基本上我需要做的是:解析字符串作爲JSON在客戶端上

$.post('map/search-submit/', { 
        data: $('#map_form').serialize() 
       }, 
        function(data) 
        { 
         console.log(data); 
         var data = $.parseJSON(response); 
         $("#tmp").tmpl(data).appendTo("#results"); 
        } 
     );