数据表是否缓存ajaxSource的结果? [英] Does datatables cache results for an ajaxSource?

查看:86
本文介绍了数据表是否缓存ajaxSource的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将jquery数据表嵌入到portlet战争文件中,并且正在经历一些有趣的行为,对此我需要一些解释.

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

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

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

这是场景.

  1. 我打开网页并在"TextA"上进行关键字搜索 结果:ajax请求发送到服务器以加载jquery数据表.

  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.

在不关闭浏览器的情况下,我对'TextB'进行了关键字搜索. 结果:ajax请求发送到服务器以加载jquery数据表.

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.

在不关闭浏览器的情况下,我再次在'TextA'上进行了关键字搜索. 结果:请求未发送到服务器.但是我的数据表足够聪明,可以记住在第1步中检索到的结果,并在页面上显示结果.

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.

如果我不得不猜测,我想在数据表中必须有一些聪明之处,可以在其中缓存参数相同的ajax源的结果,从而不必触发针对该ajax源的请求再次.

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.

我是对的吗? 我正在使用数据表1.9.4.

Am I right? I am using data tables 1.9.4.

推荐答案

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

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

但是,在代码中,您将覆盖fnServerData并使用$.getJSON(),这是 $.ajax()而不指定cache选项. cache选项的默认值为true,这就是为什么要缓存您的请求的原因.

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.

以下是jQuery手册的摘录:

Below is an excerpt from jQuery manual:

cache(默认值:truefalse表示dataType 'script''jsonp')

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

类型:布尔

如果设置为false,它将强制请求的页面不被 由浏览器缓存.注意:仅将cache设置为false 正确处理HEAD和GET请求.它通过附加工作 GET参数的"_ = {timestamp}".不需要该参数 其他类型的请求,但对URL进行POST的IE8中除外 GET已经请求的.

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.

这篇关于数据表是否缓存ajaxSource的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆