简体   繁体   English

在Jquery数据表中显示JSON数据

[英]Show JSON Data in Jquery Datatables

I've been trying to get my JSON data in jQuery DataTables component. 我一直在尝试在jQuery DataTables组件中获取JSON数据。

First I wrote the JavaScript code as well as a view as shown in the code below: 首先,我编写了JavaScript代码以及下面的代码所示的视图:

$(document).ready(function () {
    $('#myData').DataTable({
        lengthChange: false,
        ajax: {
            url: "http://amp-local/api/wipbin/FetchChild/",
            // dataSrc: 'allk'
        },
        columns: [
            { data: "name" },
            { data: "numbers" }
        ],
        select: true
    });
});

My view: 我的观点:

<table id="myData" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Numbers</th>
        </tr>
    </thead>
</table>

My JSON: 我的JSON:

[{
    "numbers": "38",
    "name": "Bllaca"
}, {
    "numbers": "28",
    "name": "Kaess"
}, {
    "numbers": "27",
    "name": "droessmer"
}, {
    "numbers": "24",
    "name": "friedricha"
}]

Result is with this error: 结果是出现此错误:

TypeError: undefined is not an object (evaluating 'f.length')

And my table is empty. 我的桌子是空的。

When using server-side processing with AJAX requests, it expects JSON data to be returned in the special format required by DataTables. 当对AJAX请求使用服务器端处理时,它期望JSON数据以DataTables所需的特殊格式返回。 The following fields of JSON response object are required: draw , data , recordsTotal and recordsFiltered . JSON响应对象的以下字段是必填字段: drawdatarecordsTotalrecordsFiltered You need to bring your server response into accordance with expected format . 您需要使服务器响应符合预期的格式
For example, your first response should be as following: 例如,您的第一个答复应如下:

{
  "draw": 1,
  "recordsTotal": 4,
  "recordsFiltered": 4,
  "data": [
    {
      "numbers": "38",
      "name": "Bllaca"
    }, {
      "numbers": "28",
      "name": "Kaess"
    }, {
      "numbers": "27",
      "name": "droessmer"
    }, {
      "numbers": "24",
      "name": "friedricha"
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM