Walter A. Jablonowski
Walter A. Jablonowski

Reputation: 349

Data requested with ajax always the same

There must be some kind of server side cache on my server...

Every time when I am requesting content using jquery ajax i get the same data, even if I replace server side code with

echo('hello, world!');

Used configuration:

What kind of caching could be active?

As you can see, I have implemented two codes to prevent browser caching within the javascript below. The problem is definitely on the server side.

Just for information:

Client side code:

// Add a timestamp to url to prevent browser from caching ajax content
var date = new Date();
var timestamp = date.getTime();
var nocache = '&nocache=' + timestamp;

// Ajax call: get entries, add to entries div
$.ajax({
    type: "POST",
    url: "jsfeed.php",
    //data: "lastmsg="+ ID,
    data: "<?php echo("key=$api_key&fc=$collection_ident&offset="); ?>" + (ID+1) + "<?php echo("&showmax=$showmax&nocontainer" . $jstaggedonly); ?>" + nocache,
    cache: false,
    success: function(html) {
        $("div#updates").append(html);
        $("#more"+ID).remove();
    }
});

Server side code:

if( isSet($_POST['offset']) && isSet($_POST['showmax']) )
{
  $offset  = $_POST['offset'];
  $showmax = $_POST['showmax'];
  $new_offset = $offset + $showmax;

  $call = $api_url . 'jsfeed.php?' . $_SERVER['QUERY_STRING'];

  $html = file_get_contents($call);
  echo($html);
  // ...
}

Edit:

If you want to have a look - the page where I'm testing (online for a limited time)... Search for a "Show more" button below updates list. This is where the ajax call is triggered.

Edit:

I solved this. There was an error in my script. There is no need to append a timestamp manually, like I did above. jQuery option cache: false is enough. So, it just looks like this:

// Ajax call: get entries, add to entries div
$.ajax({
    type: "POST",
    url: "jsfeed.php",
    data: "<?php echo("key=$api_key&fc=$collection_ident&offset="); ?>" + (ID+1) + "<?php echo("&showmax=$showmax&nocontainer" . $jstaggedonly); ?>",
    cache: false,
    success: function(html) {
        $("div#updates").append(html);
        $("#more"+ID).remove();
    }
});

Upvotes: 0

Views: 2646

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196276

You should add the timestamp to the url and not the data sent..

var nocache = '?nocache=' + timestamp;

and

$.ajax({
    type: "POST",
    url: "jsfeed.php" + nocache,

Upvotes: 0

Corbin
Corbin

Reputation: 33467

Some browsers extremely aggressively cache AJAX requests if not explicitly told not to by the server.

Try something like (taken from the php manual, php.net/headers):

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Upvotes: 1

Related Questions