我在 portlet war 文件中嵌入了 jquery 数据表,并且遇到了一些我需要解释的有趣行为。
这就是我的 javascript 的样子... http://pastebin.com/qXpwt9A7
这是场景。
结果:向服务器发送ajax请求以加载jquery数据表。
结果:向服务器发送ajax请求以加载jquery数据表。
结果:请求未发送到服务器。但是我的数据表足够聪明,可以记住在第 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
fordataType
'script'
and'jsonp'
)Type: Boolean
If set to
false
, it will force requested pages not to be cached by the browser. Note: Settingcache
tofalse
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/