数据表服务器端 ajax 和 sAjaxSource 具有不同的请求参数



我使用sAjaxSource参数制作了一个服务器端处理的数据表。

$('#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 并将user_id附加到 url 中,如下所示:

"sAjaxSource":srcUri+"?user_id="+userID+"&",

困扰我的是为什么它们有不同的请求参数格式。谁能解释一下为什么它们不同以及每种方法之间的意义是什么?

编辑:

对不起,也许我的解释不太清楚。我在这里问的不是如何在数据表ajax中传递参数。虽然我很感激提到fnServerParams,这是我需要的atm。

我在这里要问的是,为什么 sAjaxSource 和 ajax 之间的请求参数如此不同? 或者也许两种方法都有不同的目的? 我似乎无法在文档中找到它。

从文档中:

旧版本的数据表 (1.9-( 使用一组不同的参数 从服务器发送和接收。作为编写的此类脚本 对于数据表 1.10+ 将无法与数据表一起使用 1.9-.但是,DataTables 1.10 确实具有为 1.9- 编写的脚本的兼容模式。此兼容模式由使用 旧的sAjaxSource参数(而不是新的 ajax 参数( 或通过设置 $.fn.dataTable.ext.legacy.ajax = true;

当您将 sAjaxSource 与 DataTables (1.10+( 一起使用时,您将触发兼容模式,该模式发送和接收与传统数据表 (1.9-( 不同的参数集。

具有自定义 HTTP 变量的服务器端示例 - 旧版

具有自定义 HTTP 变量的服务器端示例

最新更新