Does datatables cache results for an ajaxSource?

纵饮孤独 提交于 2021-01-22 18:18:47

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30068319/does-datatables-cache-results-for-an-ajaxsource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!