datatables - 数据表是否缓存 ajaxSource 的结果?

标签 datatables jquery-datatables

我在 portlet war 文件中嵌入了 jquery 数据表,并且遇到了一些我需要解释的有趣行为。

这就是我的 javascript 的样子... http://pastebin.com/qXpwt9A7

这是场景。

  • 我打开网页并在“TextA”上进行关键字搜索
    结果:向服务器发送ajax请求以加载jquery数据表。
  • 在不关闭浏览器的情况下,我对“TextB”进行了关键字搜索。
    结果:向服务器发送ajax请求以加载jquery数据表。
  • 在不关闭浏览器的情况下,我再次对“TextA”进行关键字搜索。
    结果:请求未发送到服务器。但是我的数据表足够聪明,可以记住在第 1 步中检索到的结果并在页面上显示结果。

  • 这实际上对我很有效,但我不知道为什么会这样。

    如果我不得不猜测,我在想数据表中一定有一些智能,它可以缓存参数相同的 ajax 源的结果,这样它就不必再次触发对该 ajax 源的请求。

    我对吗?
    我正在使用数据表 1.9.4。

    最佳答案

    默认情况下,DataTables v.1.9.4 会阻止 fnServerData 函数中的请求缓存,请注意以下 DataTables 源代码摘录中的 "cache": false

      "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." );
               }
            }
         } );
      }
    

    但是,在您的代码中,您将覆盖 fnServerData 并使用 $.getJSON(),它是 $.ajax() 的速记函数,但未指定 cache 选项。 cache 选项的默认值是 true ,这就是您的请求被缓存的原因。

    以下是jQuery手册的摘录:

    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.

    关于datatables - 数据表是否缓存 ajaxSource 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30068319/

    相关文章:

    javascript - 如何使用我自己的自定义下拉菜单控制 DataTables 生成的选择菜单?

    JQuery选择表的第三列并在这个td中添加一个带有特殊类的span

    jquery - 数据表:没有分页按钮

    ruby-on-rails - ruby on rails + best_in_place + 数据表 + haml

    JQGrid、Telerik、DataTables 等哪个好?

    javascript - 在不同的 div 上对 jQuery DataTable 进行分组和列过滤

    javascript - 等同于基于 Jquery Datatables 中的基础数据进行排序的 mRender

    ruby-on-rails - 在 ruby​​ on rails 中使用 dataTable 时如何在每行添加复选框?

    javascript - 为什么这些数据表函数是 'undefined' ?