Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery XmlHttpRequest Cache-Control being ignored

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?

like image 260
HaxElit Avatar asked Mar 16 '12 21:03

HaxElit


1 Answers

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

like image 117
HaxElit Avatar answered Oct 02 '22 12:10

HaxElit