javascript - 获取自定义 Jquery 数据表 GET 将发送到服务器的请求参数

标签 javascript jquery ajax datatables json-rpc

我正在使用 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/

相关文章:

jquery - 如何使 inputCheckBoxGroup 在 Play Framework 中垂直堆叠

javascript - JQuery:使用#[objectId] 或 [id=objectId] 引用元素有什么区别

javascript - Flot:尝试在页面加载时绘制图表时弹出奇怪的错误?

javascript - 使用javascript在rails中预加载部分 View

javascript - 具有初始静态状态的 Headroom.js header

javascript - 类型错误 : Class extends value undefined is not a constructor or null in react js

javascript - Angular 表,行中的复选框

javascript - jQuery 中的 Click() 没有执行任何操作

javascript - Codeigniter-PHP : How to Change table week Dates , 更改输入日期

javascript - PhantomJS 和 CasperJS 单击链接并获取 html