如果我通过带有"cache:false"的ajax调用对一个资源发出多个请求,这是阻止浏览器使用请求头(或其他方式)缓存每个响应,还是因为_=date参数使资源url唯一,因此永远不会与缓存中的任何内容匹配,所以绕过以前缓存的响应?
作为参考,这里是关于ajax缓存属性的jquery文档:'如果设置为false,它将强制浏览器不缓存请求的页面。注意:将缓存设置为false只能正确处理HEAD和GET请求。它的工作原理是将"_={timestamp}"附加到GET参数中'
那么,它真的"强制不缓存请求的页面"吗?或者它只是通过添加日期来模仿这种效果——效果是没有两个请求是相同的,因此会发出新的请求(但一直以来,糟糕的浏览器都有一大堆缓存但从未使用过的响应)?
(如果我很难理解缓存的工作方式,请随时教我!!(我假设它基本上是一种键值类型的系统,键是请求信息,值是该请求的响应。)
是的,如果设置cache: false
,那么它只会在url+params之后附加_={timestamp}。
来自jquery src:
cacheURL = s.url;
// More options handling for requests with no content
if ( !s.hasContent ) {
// If data is available, append data to url
if ( s.data ) {
cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data );
// #9682: remove data so that it's not used in an eventual retry
delete s.data;
}
// Add anti-cache in url if needed
if ( s.cache === false ) {
s.url = rts.test( cacheURL ) ?
// If there is already a '_' parameter, set its value
cacheURL.replace( rts, "$1_=" + nonce++ ) :
// Otherwise add one to the end
cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
}
}