How to avoid a second ajax request
How to avoid a second ajax request
Doltsche
Posts: 9Questions: 0Answers: 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:
[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.
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.
This discussion has been closed.
Replies
[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
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
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