DataTables Submit data as JSON in the request body

DataTables Submit data as JSON in the request body

zackkengzackkeng Posts: 3Questions: 2Answers: 0

According to the API manual : https://datatables.net/reference/option/ajax.data, I should able to send a AJAX get request with data as JSON in the request body.

However, after I follow the same way, my data is always sent out in the request headers instead of the request body.

table = $('#table').DataTable({
dom : 'Tfrtip',
ajax : {
"url" : 'webapi/gpsinputadmin',
"contentType" : "application/json",
"data" : function(d) {
d.data = getInputData();
return JSON.stringify(d.data);
}
}
}

Is there anything wrong in my code?

Answers

  • allanallan Posts: 63,361Questions: 1Answers: 10,448 Site admin

    You need to send it a POST if you want it in the request body, since GET doesn't have one. I'll update the example. Here is a little test case: http://live.datatables.net/nurakipi/1/edit .

    Allan

  • zackkengzackkeng Posts: 3Questions: 2Answers: 0

    If GET method can't send data in request body, then the description in the manual here is abit misleading. https://datatables.net/reference/option/ajax.data

    Below is the example written in the manual.

    Submit data as JSON in the request body:

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "contentType": "application/json",
        "data": function ( d ) {
          return JSON.stringify( d );
        }
      }
    } );
    
  • allanallan Posts: 63,361Questions: 1Answers: 10,448 Site admin

    Agreed! I've committed a fix and will deploy it to the site with the next update.

    Thanks,
    Allan

This discussion has been closed.