How to avoid a second ajax request

How to avoid a second ajax request

DoltscheDoltsche Posts: 9Questions: 0Answers: 0
edited May 2013 in DataTables 1.9
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:

[code]
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 ''
}
}],
"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();
}
[/code]

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.

Replies

  • DoltscheDoltsche Posts: 9Questions: 0Answers: 0
    edited May 2013
    Meanwhile I did some further investigation on this issue and are able to provide you with some more details. I debugged the website with the IE Developer tools which let me see the call stack:

    [quote]
    fnServerData
    _fnAjaxUpdate
    _fnDraw
    _fnSort
    _fnInitialise
    Anonymous Function
    each
    each
    DataTable
    Anonymous Function
    c
    fireWith
    ready
    H
    fnServerData
    _fnAjaxUpdate
    _fnDraw
    _fnSort
    _fnReDraw
    fnDraw
    search
    onclick
    [/quote]

    Interesting here is the second call to _fnInitialize(...) which according to the comments in the code is done because "it might not have been handed of the language processor".

    [code]
    /* Check if we need to initialise the table
    * (it might not have been handed off to the language processor)
    */
    if ( bInitHandedOff === false )
    {
    _fnInitialise( oSettings );
    }
    [/code]

    I hope this lets you help me further on this issue.

    Best regards

    Samuel
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You have bServerSide: true - so every draw is going to make an Ajax request.

    It looks to me that every time you call `search()` you should get one more Ajax request. Are you saying to get two? I'm afraid that without a test case showing the problem, there probably isn't much help we can offer.

    Allan
  • DoltscheDoltsche Posts: 9Questions: 0Answers: 0
    edited May 2013
    Hello allan

    Thank you for taking the time to investigate my problem. After considering a co-worker in my company, I finally got the fault:

    [code]

    [/code]

    Sometimes, so simple things like this aren't always obvious at first.

    Best regards

    Samuel
This discussion has been closed.