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

时间:2014-06-14 10:15:47

标签: javascript jquery ajax jquery-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是现在使用onload的html表ID,它完美运行,这是它发送的请求标题

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,它必须得到一个方法和params和id键,params部分携带大块数据,即使上面显示的那个如下所示我手动使用它已经奏效了。差异包括

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服务器到我想要的。帮助apreciated。

3 个答案:

答案 0 :(得分:2)

为了自定义请求,您需要在datatable初始化参数中添加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");
        }
    });
});

答案 1 :(得分:2)

您应该可以使用以下方法添加id=1method=datatables

var oTable = $('#ip_data').DataTable( {
    "serverSide": true,
    "ajax": {
        "url": "/url",
        "data": {
            "method": "datatables",
            "id": "1",
        }
    },
});

在我读过的所有内容中,没有任何内容支持使用分号而不是&符号作为分隔符来构造查询字符串。为此,您必须推出自己的功能,请参阅此处的最后一个示例https://datatables.net/reference/option/ajax.data

另外,传递原始json有点讨厌。我的建议是让数据表自动发送参数。如果您使用'serverSide: true,'选项,数据表会自动执行此操作。

答案 2 :(得分:0)

在我的情况下。我发送以下代码

$('#transactions_table').DataTable( {
        "processing": true,
        "bLengthChange": false,
        "pageLength": 3,
        "bFilter" : false,
        "serverSide": true,
        "headers": {'Content-Type':'application/x-www-form-urlencoded'},
        "ajax":{
            data:{currency:currency_code,date_from:date_from,date_to:date_to,is_zero_fee:is_zero_fee},
            url :"http://ischool.pk",
            type: "GET"
        },"columns": [
            { "data": "transaction_id" },
            { "data": "currency" },
            { "data": "side" },
            { "data": "transaction_detail" },
            { "data": "insert_date" },
            { "data": "transaction_fee" },
            { "data": "amount" },
            { "data": "user_id" }
        ]          
    });