Yusril Maulidan Raji Yusril Maulidan Raji - 9 months ago 39
Ajax Question

How to alter ajax responses that will be handled by DataTables in ASP.NET?

As you might know, ASP.NET gives ajax response with

parameter. I tried to alter this with:

dataSrc: function (json) {
return json.d.data;

But when I run it, it shows
jquery.dataTables.js:4108 Uncaught TypeError: Cannot read property 'length' of undefined(…).
As you can see on the image I attached within this post. I checked in that part and I conclude
doesn't really alter all the response.

enter image description here

This is what I did for the DataTables's ajax option:

dataTable: {
ajax: {
type: 'POST',
url: window.location.href + '/GetData',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
dataSrc: function (json) {
return json.d.data;
..... //other options

So, is there any other option to alter all the responses that will be handled by DataTables?

Answer Source

Can you try this in dataSrc. This solution based on this link.

"dataSrc": function (data) {
    var json = $.parseJSON(data.d);

    var myData = {};
    myData.draw = parseInt(json.draw);
    myData.recordsTotal = parseInt(json.recordsTotal);
    myData.recordsFiltered = parseInt(json.recordsFiltered);
    myData.data = json.data;

    return myData.data;