Do datatables get cached for ajaxSource?
I have jquery datatables embedded inside warlet portlet file and am experiencing some funny behavior for which I need some explanation.
This is what my javascript looks like ... http://pastebin.com/qXpwt9A7
Here's the script.
-
I open a webpage and do a keyword search in 'TextA' Result: An ajax request is sent to the server to load the jQuery datatable.
-
Without closing the browser, I search for keywords in 'TextB'. Result: An ajax request is sent to the server to load the jQuery datatable.
-
Without closing the browser, I search again for keywords in 'TextA'. Result: The request is not sent to the server. But my datatable is smart enough to remember the results from step 1 and it displays the results on the page.
This works really well for me, but I don't know why this is happening.
If I had to guess, I think there should be multiple smarts in the datatables where it caches the results for the ajax source where the arguments are the same, so that it doesn't have to disable the request for that ajax source again.
I'm right? I am using data tables 1.9.4.
source to share
By default, DataTables v.1.9.4 prevents query caching in fnServerData
, see the "cache": false
excerpt from the DataTables source code below.
"fnServerData": function ( sUrl, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"url": sUrl,
"data": aoData,
"success": function (json) {
if ( json.sError ) {
oSettings.oApi._fnLog( oSettings, 0, json.sError );
}
$(oSettings.oInstance).trigger('xhr', [oSettings, json]);
fnCallback( json );
},
"dataType": "json",
"cache": false,
"type": oSettings.sServerMethod,
"error": function (xhr, error, thrown) {
if ( error == "parsererror" ) {
oSettings.oApi._fnLog( oSettings, 0, "DataTables warning: JSON data from "+
"server could not be parsed. This is caused by a JSON formatting error." );
}
}
} );
}
However, in your code, you are overriding fnServerData
and using $.getJSON()
, which is a shorthand function for $. ajax () without specifying an option cache
. The default value for parameters cache
is true
, so your requests are cached.
Below is an excerpt from the jQuery tutorial:
cache
(defaulttrue
,false
fordataType
'script'
and'jsonp'
)Type: Boolean
If set
false
, this will cause the requested pages to not be cached by the browser. Note. Settingcache
-false
will only work correctly with HEAD and GET requests. It works by adding "_ = {timestamp}" to the GET parameters. The parameter is not required for other types of requests, with the exception of IE8, when a POST is made to a URL that has already been GET requested.
source to share