Reputation: 4073
I'm using google chrome and I noticed that every time I do an XHR request I get the following headers put on the request:
Cache-Control: no-cache
Pragma: no-cache
If you read the spec at http://www.w3.org/TR/XMLHttpRequest/ it says the following
If the user agent implements a HTTP cache it should respect Cache-Control headers in author request headers (e.g. Cache-Control: no-cache bypasses the cache). It must not send Cache-Control or Pragma request headers automatically unless the end user explicitly requests such behavior (e.g. by reloading the page).
Well I'm trying the following:
$.ajax(myUrl, {
type: 'get',
dataType: 'json'
cache: true,
headers: {
'Cache-Control': 'max-age=200'
}
})
As you can see I'm explicitly setting the Cache-Control header in hopes of getting a cached copy of my resource. Well Chrome seems to ignore the Cache-Control header.
Is it possible to not send the Cache-Control: no-cache
header when making an XHR request?
Upvotes: 16
Views: 10142
Reputation: 4073
This was a dumb mistake. I had the Developer Tools set to "Disable Cache". That's why it was always adding the cache-control header. If this ever happens to you make sure you make sure that box is not checked.
Raul
Upvotes: 47
Reputation: 76746
I just tested it and your code works fine, except for a missing comma.
Open the network tab, then try this in the console on this page:
$.ajax('http://stackoverflow.com/', {
type: 'get',
dataType: 'json',
cache: true,
headers: {
'Cache-Control': 'max-age=123'
}
})
Check the network tab again, click the request you just made, and click the "headers" tab. You will see that the request was sent with the Cache-Control
header you provided.
Upvotes: 4