Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82275

How to avoid a second ajax request

$
0
0
Dear community,

After looking around I found several disussions which are also treating the problem that an ajax request is done twice when using server side processing. For example: http://datatables.net/forums/discussion/6576/datatables-table-loads-twice-first-time-without-table-data/p1

Anyway, I'm still confused about it and do not really understand why there are two ajax requests? In my case, it just causes to query the database twice which causes performance issues. My actual configuration of the datatable looks like this:

        var oTable = null;
        $(document).ready(function () {
            oTable = $('#example').dataTable({
                "bFilter": false,
                "bServerSide": true,
                "sPaginationType": "full_numbers",
                "sAjaxSource": "datareq",
                "aoColumnDefs": [{
                    "aTargets": [7],
                    "mData": 7,
                    "mRender": function (data, type, full) {
                        return '<a href=ManageAccounts.aspx?miloeid=' + data + '><img src="../Images/edit_icon_large.png"></a>'
                    }
                }],
                "aoAjaxData": [{ "name": "sCustomSearch0", "value": "Marco" }, { "name": "sCustomSearch1", "value": "Pedrazzini" }],
                "fnServerData": function (sSource, aoData, fnCallback, oSettings) {
                    if (oSettings.oInit.aoAjaxData !== undefined && oSettings.oInit.aoAjaxData != null) {
                        if (Array.isArray(oSettings.oInit.aoAjaxData)) {
                            aoData = aoData.concat(oSettings.oInit.aoAjaxData);
                        } else {
                            aoData.push(oSettings.oInit.aoAjaxData);
                        }
                    }   
                    oSettings.jqXHR = $.ajax({
                        "dataType": "json",
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    })
                }
            });
        });
        function search() {
            oTable.fnSettings().oInit.aoAjaxData = [{ "name": "sFirstname", "value": $('#DefaultContent_PersonFirstnameSearchTextBox').val() },
                { "name": "sLastname", "value": $('#DefaultContent_PersonLastnameSearchTextBox').val() },
                { "name": "sNumber", "value": $('#DefaultContent_PersonNumberSearchTextBox').val() },
                { "name": "sInsuranceNumber", "value": $('#DefaultContent_PersonInsuranceNbrSearchTextBox').val() }];
            oTable.fnDraw();
        }

How can I tell the datatables to do the request only once or what would be a clean way to distinguish on the server side the first request from the second request?

Kind regards

Samuel

P.s: Unfortunately I can not post a link to the website because its an intranet project.

Viewing all articles
Browse latest Browse all 82275

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>