简体   繁体   中英

Selectively Force Browser Cache to Clear Ajax Request

There are a lot of answers out there for browser caching but most are about either turning caching on or off or requesting using a different url for some static asset like js or images.

  • I am loading json data over an ajax request
  • This is dynamic content, say a list of tasks
  • I only want to update when a user has done a certain action. Until then it can be cached forever.
  • When the user takes that action, I want to invalidate the cache and get a new version from the server.

    1. Is there a way to clear the browsers local cache without changing the url. So, /tasks is cached until next year. I want to invalidate that and force future requests to /tasks to get new data and a new expires header.

    2. Would I be better off not allowing browser caching and cache the requests/data on the server instead?

Another similar question: Forcing AJAX request to revalidate cache with server, without reloading completely

foreach request with cache: false jquery will add a timestamp as a get paramater, so the request is unique and won't be cached

$.ajax({
    url:"example.php"
    cache:false
});

referring to the jquery docs : cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM