Javascript Datatables服务器端ajax和sAjaxSource具有不同的请求参数
我使用sAjaxSource参数制作了一个带有服务器端处理的数据表Javascript Datatables服务器端ajax和sAjaxSource具有不同的请求参数,javascript,datatable,datatables,Javascript,Datatable,Datatables,我使用sAjaxSource参数制作了一个带有服务器端处理的数据表 $('#tbl-kamus').DataTable({ "processing": true, "bServerSide": true, "sAjaxSource": srcUri , ... 当它向服务器发出请求时,它将使用以下参数发送GET数据: sEcho: 1 iColumns: 4 sColumns: ,,,,,, iDisplayStart: 0 iDisplayLength: 30
$('#tbl-kamus').DataTable({
"processing": true,
"bServerSide": true,
"sAjaxSource": srcUri ,
...
当它向服务器发出请求时,它将使用以下参数发送GET数据:
sEcho: 1
iColumns: 4
sColumns: ,,,,,,
iDisplayStart: 0
iDisplayLength: 30
mDataProp_0: 0
sSearch_0:
bRegex_0: false
bSearchable_0: true
bSortable_0: false
mDataProp_1: 1
sSearch_1:
bRegex_1: false
bSearchable_1: true
bSortable_1: true
mDataProp_2: 2
sSearch_2:
bRegex_2: false
bSearchable_2: true
bSortable_2: true
mDataProp_3: 3
sSearch_3:
bRegex_3: false
bSearchable_3: true
bSortable_3: false
sSearch:
bRegex: false
iSortCol_0: 1
sSortDir_0: desc
iSortCol_1: 2
sSortDir_1: desc
iSortingCols: 2
并使用php中的GET参数来处理请求。
然后我需要向请求添加更多参数,因此我将选项更改为
$('#tbl-kamus').DataTable({
"processing": true,
"bServerSide": true,
"ajax": {
"url": srcUri,
"data": {
"user_id": userID
}
...
整个请求参数变成了类似数组的东西:
draw: 1
columns[0][data]: 0
columns[0][name]:
columns[0][searchable]: true
columns[0][orderable]: false
columns[0][search][value]:
columns[0][search][regex]: false
columns[1][data]: 1
columns[1][name]:
columns[1][searchable]: true
columns[1][orderable]: true
columns[1][search][value]:
columns[1][search][regex]: false
columns[2][data]: 2
columns[2][name]:
columns[2][searchable]: true
columns[2][orderable]: true
columns[2][search][value]:
columns[2][search][regex]: false
columns[3][data]: 3
columns[3][name]:
columns[3][searchable]: true
columns[3][orderable]: false
columns[3][search][value]:
columns[3][search][regex]: false
order[0][column]: 1
order[0][dir]: desc
order[1][column]: 2
order[1][dir]: desc
start: 0
length: 30
search[value]:
search[regex]: false
user_id: 2
因此,我必须更改服务器端脚本以适应这种情况,这是我避免的。然后,作为一种解决方法,我继续使用sAjaxSource并将用户id附加到url中,如下所示:
"sAjaxSource":srcUri+"?user_id="+userID+"&",
困扰我的是,为什么它们有不同的请求参数格式。谁能解释一下为什么它们不同,每种方法之间的意义是什么
编辑:
对不起,也许我的解释不太清楚。这里我要问的不是如何在datatable ajax中传递参数。尽管我非常感谢您提到fnServerParams,这正是我需要的
我在这里要问的是,为什么sAjaxSource和ajax之间的请求参数如此不同?
或者这两种方法有不同的目的?
我似乎在文档中找不到这方面的信息。来自:
旧版本的DataTables(1.9-)使用了一组不同的参数
从服务器发送和接收。因此,编写的脚本
对于DataTables,1.10+将无法与DataTables一起运行
1.9-. 但是,DataTables 1.10对于为1.9-编写的脚本有一个兼容模式。此兼容模式由使用触发
旧的sAjaxSource参数(而不是新的ajax参数)
或者通过设置$.fn.dataTable.ext.legacy.ajax=true
当您将sAjaxSource与DataTables(1.10+)一起使用时,您触发了兼容模式,它发送和接收的参数集与传统DataTables(1.9-)不同
使用
fnServerParams
而不是以编程方式添加它们。您确实无法避免使用数组样式,因为DT就是这样传回的,例如对某个特定列进行排序。这一切都在仁慈的上帝那里得到了很好的记录!文件上一直都有,我一定是瞎了。感谢jeetazBTW,我更喜欢使用$.fn.dataTable.ext.legacy.ajax=true代码>以及ajax:{…}
直到我更新服务器端代码以解析新格式而不是旧格式。这样,我就可以在前端完成更新,只需去掉$.fn.dataTable.ext.legacy.ajax=true代码>。谢谢你,我的朋友friend@Fr0zenFyr-很高兴有帮助!(y)