Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does datatables cache results for an ajaxSource?

I have embedded jquery datatables inside a portlet war file and am experiencing some funny behaviour which I need some explanation for.

This is what my javascript looks like...http://pastebin.com/qXpwt9A7

Here is the scenario.

  1. I open the webpage and do a keyword search on 'TextA' Result: A ajax request is sent to the server to load the jquery datatable.

  2. Without closing the browser I do a keyword search on 'TextB'. Result: A ajax request is sent to the server to load the jquery datatable.

  3. Without closing the browser I do a keyword search on 'TextA' again. Result: A request is not sent to the server. But my datatable is smart enough to remember the results retrieved in step 1 amnd it displays the results on the page.

This actually works well for me but I don't know why it is happening.

If I had to guess I'm thinking there must be some smarts in datatables where it caches the results for an ajax source where the arguments are the same so that it doesn't have to fire off the request for that ajax source again.

Am I right? I am using data tables 1.9.4.

like image 944
Richie Avatar asked Dec 20 '22 04:12

Richie


1 Answers

By default, DataTables v.1.9.4 prevents request caching in fnServerData function, notice "cache": false in 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're overriding fnServerData and using $.getJSON() which is a shorthand function for $.ajax() without specifying cache option. Default value for cache options is true, that is why your requests are being cached.

Below is an excerpt from jQuery manual:

cache (default: true, false for dataType 'script' and 'jsonp')

Type: Boolean

If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

like image 163
Gyrocode.com Avatar answered Dec 21 '22 16:12

Gyrocode.com