1

我有这个数据:

[
{
    "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是我上面写的json数据。

我得到的只是没有数据的空 html 标签。

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

我究竟做错了什么?

4

1 回答 1

0

我发现了问题所在:

返回的数据被视为字符串(我使用 PHP 的 json_encode($data); 发送它)

所以基本上我需要做的是:在客户端将字符串解析为 JSON

$.post('map/search-submit/', {
                    data: $('#map_form').serialize()
                },
                    function(data)
                    {
                        console.log(data);
                        var data = $.parseJSON(response);
                        $( "#tmp" ).tmpl( data ).appendTo( "#results" );
                    }
        );
于 2012-12-16T16:02:04.233 回答