jQuery ajaxSetup({cache: true}) 通常有效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2765476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does jQuery ajaxSetup({cache: true}) generally work?
提问by
jQuery 1.4.2 omits the timestampGET parameter (to defeat browser cacheing) if I assert the ajax cache setting in the local context:
如果我在本地上下文中断言 ajax 缓存设置,jQuery 1.4.2 会省略时间戳GET 参数(以破坏浏览器缓存):
$.ajax({
url: searcher,
data: keys,
cache: true,
type: 'GET',
dataType: 'json',
success: function(data) {
// something
});
But it includes timestamp if I move the setting out of there and into the global context:
但是,如果我将设置从那里移到全局上下文中,则它包括时间戳:
$.ajaxSetup({cache: true});
Moreover, if I let the default apply, jQuery sets timestamp, which doesn't seem to match the manual.
此外,如果我让默认应用,jQuery 设置timestamp,这似乎与手册不匹配。
Do you experience the same?
你有同样的经历吗?
Do HTTP cache control response headers from the server affect this jQuery feature?
来自服务器的 HTTP 缓存控制响应标头是否会影响此 jQuery 功能?
采纳答案by James Lawruk
Looks like it works. The following three ajax calls only passes a timestamp as a parameter in the 2nd case. The name of the timestamp parameter is not timestamp, but rather an underscore.
看起来它有效。以下三个 ajax 调用仅在第二种情况下传递时间戳作为参数。时间戳参数的名称不是时间戳,而是下划线。
$.ajax({ url: '/?=testDefault',
data: { 'cache': 'default' }
});//no timestamp
$.ajaxSetup({ cache: false });
$.ajax({ url: '/?=testFalse/',
data: { 'cache': 'false' }
});//yes, a timestamp
$.ajaxSetup({ cache: true });
$.ajax({ url: '/?=testTrue/',
data: { 'cache': 'true' }
}); //no timestamp
As an aside, are you using an autocomplete plugin? That passes a timestampparameter by default. You can override it using the extraParams option by passing something like this.
顺便说一句,您是否使用自动完成插件?默认情况下传递时间戳参数。您可以使用 extraParams 选项通过传递类似的内容来覆盖它。
extaParams: {timestamp:'cache'}
回答by Trafalmadorian
You can manually add a timestamp as a get parameter, that would be a totally fine workaround right?
您可以手动添加时间戳作为获取参数,这将是一个非常好的解决方法,对吗?
function myAjaxFunction()
{
var tS=new Date().getTime();
$.ajax({
url: searcher,
data: {timestamp:tS},
cache: true,
type: 'GET',
dataType: 'json',
success: function(data) {
//something here
}});
}