This might be a stupid question, but I searched a lot without any result. I know how to setup cache-control in server responses, but how can I change the cache control value in an ajax request? NOTE: I want the browser to use it's cache. I don't want it to get the updated json from the server ... this is the whole thing I'm trying to do.
There is however a better solution – caching AJAX requests. Although we can use a standard caching solution provided by HTTP (yes, Ajax is cached by HTTP), there is a catch: It works for GET requests only (not POST). Furthermore, you cannot control cache expiration on the application side.
cache:true is the default and does not always get the content from the cache. The cache-ability of an item on the browser is determined by: The response headers returned from the origin web server. If the headers indicate that content should not be cached then it won't be.
The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use. We can use $. ajaxSetup({cache:false}); to apply the technique for all AJAX functions.
You can use headers
property, like this:
$.ajax({
...
headers: {
'Cache-Control': 'max-age=1000'
}
...
});
Keep in mind that cache
property has nothing in common with Cache-Control
header, it's just a cache buster (appending ?_={timestamp}
to GET parameters) and will only work correctly with GET
and HEAD
requests.
Anyway, something useful: How to set HTTP headers (for cache-control)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With