Does jQuery ajaxSetup({cache: true}) generally work?
jQuery 1.4.2 omits the timestamp GET par开发者_运维问答ameter (to defeat browser cacheing) if I assert the ajax cache setting in the local context:
$.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.
Do you experience the same?
Do HTTP cache control response headers from the server affect this jQuery feature?
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({ 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 timestamp parameter by default. You can override it using the extraParams option by passing something like this.
extaParams: {timestamp:'cache'}
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
}});
}
精彩评论