首页 > 解决方案 > 成功响应的表中没有可用数据

问题描述

我检查了 Firefox 中的网络监视器,它说我成功获取了 Json 数据。从弹簧指南来看,它应该是:

{
  "id": 37658,
  "content": "Hello, World!"
}

但我得到no data available in table了我的数据表。我错过了什么吗?

Plunker的完整代码

桌子:

    <table id="example" class="display" style="width: 100%">
        <thead>
            <tr>
            </tr>
        </thead>
    </table>

脚本:

<script>

$(document).ready(function() {
    $('#example').DataTable({
        "ajax" : {
            "dataType" : 'json',
            "contentType" : "application/json; charset=utf-8",
            "url" : "http://rest-service.guides.spring.io/greeting",
            "dataSrc" : "",
        },
        columns: [{
            data: "id",
            name: "id",
            title: "Id"
            }, 
         {
            data: "content",
            name: "content",
            title: "content"
        }]
    });         
});

</script>

标签: jqueryhtmljsondatatabledatatables

解决方案


我的列语法不正确。它通过一些附加参数固定在下面:

<script>
$(document).ready(function() {
        $('#example').DataTable({
            "ajax" : {
                "dataType" : 'json',
                "contentType" : "application/json; charset=utf-8",
                "url" : "http://rest-service.guides.spring.io/greeting",
                "dataSrc" : "",
            },
        "columns": [
            { "data": "id"},
            { "data": "content"}
        ]
        });         
    }); 
</script>

推荐阅读