Jquery DataTable passing a parameter to ajax call

2019-09-12 17:16发布

问题:

below is my code in which I send an ajax call to my web method and get data from server to populate my HTML table, as you can see I am using Jquery DataTables to accomplish the task, and it is working fine

  $('#example').DataTable({
            "ajax": {
                "dataType": 'json',
                "contentType": "application/json; charset=utf-8",
                "type": "POST",
                "url": "index.aspx/Risky",
                "dataSrc": function (json) {
                    return $.parseJSON(json.d);
                }
            },
            "columns": [
                { "data": "Prctice_Group_Risk_No" },
                { "data": "Practice_Group" },
                { "data": "Risk_Category" },
            ]
        });

my question is how can i pass a parameter with this ajax call? I have seen everywhere on net but all those examples are about where its server side processing but here i am am using client side processing , I am not using fnsServerData or fnServerParams , Can anyone help me know how to pass a parameter with my ajax call?

回答1:

Use ajax.data option to add or modify data submitted to the server upon an Ajax request.

$('#example').DataTable({
   "ajax": {
       "dataType": 'json',
       "contentType": "application/json; charset=utf-8",
       "type": "POST",
       "url": "index.aspx/Risky",
       "data": function (d) {
          d.extra_search = $('#extra').val();
       },
       "dataSrc": function (json) {
           return $.parseJSON(json.d);
       }
    },
    "columns": [
        { "data": "Prctice_Group_Risk_No" },
        { "data": "Practice_Group" },
        { "data": "Risk_Category" },
     ]
});