views:

609

answers:

3

I'm having a peculiar problem:

PHP SCRIPT:

// checks If-Modified-Since header (if nothing has changed)
// Sends HTTP/1.0 304 Not Modified
// Sends Cache-control: public, must-revalidate
// exits

// if NO If-Modified-Since or something has changed
// builds content
// Sends Last-Modified: [DATE TIME]
// Sends Cache-control: public, must-revalidate
// exits

I am using jQuery AJAX to attempt to refresh the content on demand, I am trying to do this by altering the If-Modified-Since header with a DATE TIME in the past, using the "beforeSend" param.

This is currently what happens:

  1. Request is made, 200 OK response with fresh content
  2. Subsequent requests get a 304 response and browser pulls from cache
  3. CONTENT IS CHANGED on the server
  4. A request is made with modified If-Modified-Since header (no URL string altering), 200 OK response with fresh content
  5. Subsequent requests (without altering If-Modified-Since header) seems to retrieve old content from step #1

I guess im under the impression that at step #4, the browsers content should have been refreshed, what am i missing?

I setup a little test so you can try and see what i mean: http://tweetplenty.com/test/test2.php ... use FF and firebug if you can as im using console.log() ... here is the order of operations:

  1. the "normal" link will do a request to http://tweetplenty.com/test/test.php without modifying if-modified-since ... subsequent requests using the "normal" link should return a 304 (it should return 200 OK every 60 seconds)

  2. the "if-modified-since" link will do a request to http://tweetplenty.com/test/test.php WITH modified if-modified-since header, the browser will return new content.

  3. clicking "normal" again at this point returns the previously cached data, I would think that it should have refreshed the data after clicking the "if-modified-since" link.

If you want to checkout the test scripts themselves here you go: http://tweetplenty.com/test/test.zip

A: 

Sounds like some level of caching between the jquery script and the PHP script. I would suspect browser-level caching. (although it could theoretically be proxy caching, I guess)

Have you tried using curl or a web browser to try your AJAX request manually?

Jason S
Its definitely browser level.. try hitting ctrl f5
jim
+1  A: 

I think you want to modify the headers on all requests coming in. Check out example #2 on this page.

Andy Gaskell
A: 

Hi,

when making requests via the XmlHttpRequest to get any modified content from your server script you should append some variable that changes at the end of the querystring.

For example if your script is

mydomain.com/myscript.php

then a way to call it via let's say Jquery Ajax is to make a request at

mydomain.com/myscript.php?t=[some_value].

Usually [t] can be the current time in milliseconds or an other number.

In other words try to create a unique URL each time you send a request via the XMLHttpRequest Object.

As for the Cache-Control header there are more options that you should have a look at.

Regards,

andreas
He doesnt want to do that, we've already talked about it here http://stackoverflow.com/questions/1301922/how-can-i-invalidate-a-page-in-the-browser-cache-from-the-client-side/1301944#1301944
Cleiton