Overwrite Ajax Data for server side processing

Overwrite Ajax Data for server side processing

jmf191jmf191 Posts: 3Questions: 1Answers: 0

Hello, I used DataTables heavily in the past, but I'm new 1.10. I love it so far and was excited to get on a project that could make use of this awesome plugin. Anyways, my question is can I overwrite data sent in my ajax call? I don't really need all of the info the default sends and I already have a ajax call that works. I know I've done something similar in the past with older versions of DataTables but don't remember what because I can't look at the code any more, it's been awhile, and with the new version changes.

Example:

I have the following ajax call that works:

return $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url:  "Service/GetStuff",
                data: getCriteria(),
                dataType: "json",
                cache: false
            });

Where getCriteria function is something like:

var criteria = {
                "Model": $("#ModelDl").val(),
                "Year": $("#YearDl").val(),
                "Vendor": $("#VendorDl").val(),
                "PageStart": pageStart, //I'm grabbing page and sort info from page.info() and sort.info()
                "PageEnd": pageEnd,
                "Sort": sort,
                "SortDirection": sortDirection
            }

            return '{"criteria":' + JSON.stringify(criteria) + '  }';

This hits a WCF service.

So, I thought I could easily just jam the above ajax call into the DataTable ajax, modify the WCF service to return what DataTables is expecting like this:

table = $("#ResultsTbl").DataTable({
                "order": [[0, "asc"]],
                "pageLength": 50,
                "serverSide": true,
                "ajax": {
                       type: 'POST',
                       contentType: 'application/json; charset=utf-8',
                       url:  "Service/GetStuff",
                       data: getCriteria(),
                       dataType: "json",
                       cache: false
                },
                "columns": [
                            { "data": "ItemNumber" },
                            { "data": "DownloadDateTime" },
                            { "data": "Size" }
                ]
            });

But this immediately throws "Object doesn't support this property or method" in MicrosoftAjax.js. It says a "replace" function is undefined. Pretty vague issue, I know, but is this not possible or am I doing it wrong? Thanks in advance for any help.

Answers

  • jmf191jmf191 Posts: 3Questions: 1Answers: 0

    So, I looked into ajax.dataSrc, and it sounded like what I'm suppose to be using for this. I put it in like so:

    table = $("#ResultsTbl").DataTable({
                    "order": [[0, "asc"]],
                    "pageLength": 50,
                    "serverSide": true,
                    "ajax": {
                           type: 'POST',
                           contentType: 'application/json; charset=utf-8',
                           url:  "Service/GetStuff",
                           dataSrc: getCriteria(),
                           dataType: "json",
                           cache: false
                    },
                    "columns": [
                                { "data": "ItemNumber" },
                                { "data": "DownloadDateTime" },
                                { "data": "Size" }
                    ]
                });
    

    It now hits the WCF service, but it's sending the default DataTable data, which is not what the service is expecting. It's not even including ajax.dataSrc, so I must not fully understand it's function.

  • jmf191jmf191 Posts: 3Questions: 1Answers: 0

    Maybe I should get another cup of coffee, this worked and should have been my first attempt/thought:

    table = $("#ResultsTbl").DataTable({
                    "order": [[0, "asc"]],
                    "pageLength": 50,
                    "serverSide": true,
                    "ajax": {
                           type: 'POST',
                           contentType: 'application/json; charset=utf-8',
                           url:  "Service/GetStuff",
                           data: function(d) {
                                  return getCriteria();
                           },
                           dataType: "json",
                           cache: false
                    },
                    "columns": [
                                { "data": "ItemNumber" },
                                { "data": "DownloadDateTime" },
                                { "data": "Size" }
                    ]
                });
    

    Oops, sorry, everyone. Thanks again!

This discussion has been closed.