Rikk Mor
Rikk Mor

Reputation: 75

How to send manual payload in jQuery Datatable?

I was working on jQuery data table without server-side caching and pagination earlier, but now I am getting API that is performing server-side pagination searching and sorting.

I have some idea about server-side functionality provided by data table but my question is how can I pass my own parameters to ajax request, I have noticed that jQuery Datatable sends a payload that contains many things but I need to send some extra information as well to the backend.

My Code-

 $('#dataTable').ready(function () {
    $('#datatable').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax":{
    "url": "API/GetDetails",
    "dataType": 'json',
    "type": "GET",
    "beforeSend": function(xhr){
     xhr.setRequestHeader("Authorization",
    "Bearer Token");
   }
  }
});

})

Is there any way to change the payload values sent to the backend while making Api request? If yes how and where I should need to edit the code.

Upvotes: 0

Views: 990

Answers (1)

localhost
localhost

Reputation: 513

You can send custom http variables as per their documentation

https://datatables.net/examples/server_side/custom_vars.html

ex:

$(document).ready(function() {
$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "scripts/server_processing.php",
        "data": function ( d ) {
            d.myKey = "myValue";
            // d.custom = $('#myInput').val();
            // etc
        }
    }
} );

} );

Upvotes: 1

Related Questions