鉴于使用Django 1.10和Django-Tastypie 0.13.3构建的RESTful API,它对GET请求给出以下响应:
{
"meta": {
"limit": 20,
"next": "/api/v1/serverwinpatchresults/?limit=20&offset=20",
"offset": 0,
"previous": null,
"total_count": 178
},
"objects": [
{
"date_installed": true,
"downloaded": "True",
"installed": "True",
"patch_guid": {
"dateinserted": "2016-08-14T09:34:47",
"description": "https://support.microsoft.com/en-us/kb/2757638",
"guid": "013af7d6-ec9f-4def-ae41-007d211f9fbc",
"kb": 0,
"needsreboot": "False",
"resource_uri": "/api/v1/winpatches/013af7d6-ec9f-4def-ae41-007d211f9fbc/",
"severity": "Moderate",
"title": "Security Update for Windows Server 2008 R2 x64 Edition (KB2757638)",
"userinput": "False"
},
"patch_result_id": "16013af7d6-ec9f-4def-ae41-007d211f9fbc",
"resource_uri": "/api/v1/serverwinpatchresults/16013af7d6-ec9f-4def-ae41-007d211f9fbc/",
"server": {
"active": 1,
"cpuarch": null,
"customer": {
"active": 1,
"customer_id": 1,
"name": "SOMENAME",
"resource_uri": "/api/v1/customers/1/"
},
"customer_0": null,
"customername": null,
"domain": null,
"fqdn": null,
"host": null,
"hwaddr_interfaces": null,
"ip4": null,
"ip6": null,
"kernel": null,
"kernelrelease": null,
"master": null,
"mem_total": null,
"nodename": "SOMENODENAME",
"num_cpus": null,
"num_gpus": null,
"os": {
"active": 1,
"name": "Windows",
"os_id": 1,
"resource_uri": "/api/v1/operatingsystems/1/"
},
"os_0": null,
"os_family": null,
"osrelease": null,
"path": null,
"resource_uri": "/api/v1/servers/16/",
"saltversion": null,
"server_id": 16,
"shell": null,
"tag_name": "SOMETAGNAME",
"timezone": null,
"uuid": null,
"windows_domain": null,
"wintel": null,
"zmqversion": null
}
},
...
]
}
以下HTML:
<table id="patch_report" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Server</th>
<th>Guid</th>
<th>Downloaded</th>
<th>Installed</th>
<th>Date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
以下JavaScript:
<script>
$(document).ready(function() {
$('#patch_report').DataTable( {
"serverSide": true,
"ajax": {
"type" : "GET",
"url": "http://localhost:8000/api/v1/serverwinpatchresults/",
"dataSrc": "objects"
},
"columns": [
{ "data": "server.nodename" },
{ "data": "patch_guid.guid" },
{ "data": "downloaded" },
{ "data": "installed" },
{ "data": "date_installed" }
]
} );
} );
</script>
我试图弄清楚为什么我的表无法渲染。它不仅无法渲染,而且无法在没有错误的情况下渲染。
当我查看PyCharm中Django dev服务器发出的请求时,我看到以下内容:
&#34; GET /api/v1/serverwinpatchresults/?draw=1&columns%5B0%5D%5Bdata%5D=server.nodename&columns%5B0%5D%5Bname%5D=&columns%5B0%图5D%5Bsearchable%5D =真安培;柱%5B0%5D%5Borderable%5D =真安培;柱%5B0%5D%5Bsearch%5D%5Bvalue%5D =安培;柱%5B0%5D%5Bsearch%5D%5Bregex%5D =假安培;柱%5B1%5D%5Bdata%5D = patch_guid.guid&安培;柱%5B1%5D%5Bname%5D =安培;柱%5B1%5D%5Bsearchable%5D =真安培;柱%5B1%5D%5Borderable%5D =真安培;柱%5B1%5D%5Bsearch%5D%5Bvalue%5D =安培;柱%5B1%5D%5Bsearch%5D%5Bregex%5D =假安培;柱%5B2%5D%5Bdata%5D =下载&安培;柱%5B2%图5D%5Bname%5D =安培;柱%5B2%5D%5Bsearchable%5D =真安培;柱%5B2%5D%5Borderable%5D =真安培;柱%5B2%5D%5Bsearch%5D%5Bvalue%5D =安培;柱% 5B2%5D%5Bsearch%5D%5Bregex%5D =假安培;柱%5B3%5D%5Bdata%5D =装&安培;柱%5B3%5D%5Bname%5D =安培;柱%5B3%5D%5Bsearchable%5D =真安培;列%5B3%5D%5Borderable%5D =真安培;柱%5B3%5D%5Bsearch%5D%5Bvalue%5D =安培;柱%5B3%5D%5Bsearch%5 d%5Bregex%5D =假安培;柱%5B4%5D%5Bdata%5D =日期和安培;柱%5B4%5D%5Bname%5D =安培;柱%5B4%5D%5Bsearchable%5D =真安培;柱%5B4%5D% 5Borderable%5D =真安培;柱%5B4%5D%5Bsearch%5D%5Bvalue%5D =安培;为了%5B0%5D%5Bcolumn%5D = 0&安培;图5D%5Bsearch%5D%5Bregex%5D =假安培列%5B4%;订单%5B0%5D%5Bdir%5D = asc&amp; start = 0&amp; length = 10&amp; search%5Bvalue%5D =&amp; search%5Bregex%5D = false&amp; _ = 1471378715102 HTTP / 1.1&#34; 200 30115
对我来说,似乎从Datatables到我的API的GET请求正在工作并返回某些东西。但是,除了搜索栏,条目数选择器和表头之外,不会呈现任何内容。我以为我可以使用columns
从API中提取我想要的数据,以深入查找我需要的确切字段。然而,情况似乎并非如此。
所以这是一个操作员错误。看来我在localhost
加载页面时通过127.0.0.1
调用API,因此跨域来源正在开始。
现在的问题是分页不起作用,也不过滤。我相信Brian Kates是正确的,我没有通过数据表消耗API中的元数据,例如总记录数和过滤记录数,但是我无法在文档中找到有关如何实现的可靠示例此
答案 0 :(得分:1)
为了简单起见,我首先关闭服务器端。它与ajax数据源无关,需要您返回额外的参数。例如,您需要返回总记录数和筛选记录数。我在你的代码中没有看到它。请参阅文档:https://datatables.net/examples/server_side/。因为你已经省略了这些参数,这可能就是你没有看到任何东西的原因。
编辑:查看服务器端处理文档:https://datatables.net/manual/server-side#Sent-parameters。分页通过在发送的参数中发送开始和长度来工作。您显然需要使用该信息来查询您的数据。
对于服务器端过滤,您需要设置记录总数和记录过滤。