我正在使用 Jquery 数据表,我希望在加载页面时它向服务器发送自定义 GET 请求,而不是它发送的默认 GEt 请求。这是它在加载时发送请求的 JS 部分
$(document).ready(function() {
oTable=$('#ip_data').dataTable({
"bProcessing": true,
"bServerSide": true,
"bPaginate": true,
"bScrollCollpase": true,
"sScrollY": "200px",
"sAjaxSource": "/url"
});
});
其中 #ip_data 是 html 表 ID,现在使用 onload 它可以完美工作,这是它发送的请求 header
http://domain.com/getdata?sEcho=1&iColumns=5&sColumns=%2C%2C%2C%2C&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&sSe arch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&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=true&mDataProp_4=4&sSearch_4=&bRegex_4=false&bSearchable_4=true&bSortable_4=true&sSearch=&bRegex=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&_=1402738395413
现在我正在使用的服务器是 Rpc,它必须获得一个方法和参数以及 id 键,其中参数部分携带大量数据,即将上面显示的内容变成我手动设置的如下所示的内容使用过并且有效。差异包括
1 the addition of method=method
2 separation of method ,params and id by use of ';'
3 params={...} which is valid json
4 id=1
http://domain.com:5000/?method=datatables;params={"sEcho":"1", "iColumns":"5", "sColumns":",,,,", "iDisplayStart":"0", "iDisplayLength":"10", "mDataProp_0":"0", "sSearch_0":"", "bRegex_0":"false", "bSearchable_0":"true", "bSortable_0":"true", "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":"true", "mDataProp_4":"4", "sSearch_4":"", "bRegex_4":"false", "bSearchable_4":"true", "bSortable_4":"true", "sSearch":"", "bRegex":"false", "iSortCol_0":"0", "sSortDir_0":"asc", "iSortingCols":"1" };id=1
我知道 js 和 jquery 可以执行上述操作,但我没有成功使用 jquery 数据表,因为我无法从哪里向 te 服务器发出我想要的自定义请求。感谢帮助。
最佳答案
为了自定义请求,您需要在数据表初始化参数中添加 fnServerData
,该函数具有自定义的 ajax 调用,如下所示。您可以访问来源,aoData
中的默认数据,回调函数和设置,现在您可以像这样添加自己的自定义数据,
aoData["MyCustomValue"] = 123;
或者您可以简单地覆盖 aoData
对象,由您决定
我已经在这里发帖了。
$(document).ready(function() {
oTable=$('#ip_data').dataTable({
"bProcessing": true,
"bServerSide": true,
"bPaginate": true,
"bScrollCollpase": true,
"sScrollY": "200px",
"sAjaxSource": "/url",
"fnServerData": function (sSource, aoData, fnCallback, oSettings) {
aoData["MyCustomValue"] = 123;
oSettings.jqXHR = $.post(sSource, aoData, fnCallback, "json");
}
});
});
关于javascript - 获取自定义 Jquery 数据表 GET 将发送到服务器的请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24218867/