Tuesday, December 25, 2018

How to post json string for datatables framework

By default, the Ajax request that DataTables makes to obtain server-side processing data is an HTTP GET request. However, there are times when you might wish to use POST. This is very easily done by using the type option of the ajax initialisation option. Naturally, you might want to post your data as a json string within the body of this ajax call. Here comes the important attributes of the ajax option for that scenario.
$('.my-datatable-div').DataTable( {
  "ajax": {
    "contentType": "application/json",
    "type": "POST",
    "data": function(d) {
      return JSON.stringify({"languages" : ["javascript","golang"]});