问题描述
我正在使用 Datatables 插件通过ajax属性从服务器获取表数据,并使用dataSrc属性.我的数据表定义:
I'm using the Datatables plugin to get table data from a server using the ajax Property and transform it using the dataSrc property. My datatables definition:
var my_table = $('#my_table').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/my/url",
"type": "POST",
"dataSrc": function(json) {
console.log('json', json);
return format_my_table_data(json.data);
}
},
"deferLoading": found_rows,
"data": initial_my_table_data,
"ordering": true,
"order": [[1, "desc"]],
"lengthMenu": [
[25, 50, 100],
[25, 50, 100]
],
"columns": my_table_columns
});
初始加载工作正常,有时对列进行排序和搜索也可行,但大多数情况下,我看到带有有效json的ajax调用成功返回(我使用 jsonlint.com ),有时表停留在正在处理...",或者表根本没有变化.
The initial load works fine, and ordering columns and searching works sometimes but mostly I see the ajax call return successfully with valid json (I verified using jsonlint.com) and the table is stuck at "Processing..." sometimes or the table just doesn't change at all.
来自服务器的Json:{"recordsTotal":379,"recordsFiltered":378,"draw":25,"data":[{...}]}
Json from the server:{"recordsTotal":379,"recordsFiltered":378,"draw":25,"data":[{...}]}
在控制台中检查网络时,我可以看到发送的ajax请求和每次响应都相似(总时间在600ms到1600ms之间,具体取决于返回的行数),但是我的console.log位于不会调用dataSrc函数,并且控制台中没有javascript错误.有什么作用?
When inspecting the network in the console i can see the ajax request being sent and a response that is similar each time (with a total time between 600ms and 1600ms depending on the number of rows returned) but my console.log inside the dataSrc function isn't called and no javascript errors in the console. What gives?
推荐答案
您的脚本应在请求中返回与draw
参数相同值的draw
参数.我相信它从1
开始,然后随着每个请求而递增.
Your script should return draw
parameter with the same value of the draw
parameter in the request. I believe it starts at 1
and then increments with every request.
来自手册:
此对象是对-的响应的绘制计数器,它来自作为数据请求一部分发送的draw
参数.请注意,出于安全原因强烈建议,将此参数转换为整数,而不是简单地将其在draw
参数中发送的内容回显给客户端,以防止跨站点脚本执行(XSS)攻击.
The draw counter that this object is a response to - from the draw
parameter sent as part of the data request. Note that it is strongly recommended for security reasons that you cast this parameter to an integer, rather than simply echoing back to the client what it sent in the draw
parameter, in order to prevent Cross Site Scripting (XSS) attacks.
这篇关于成功的Ajax响应未调用datatables dataSrc函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!