使用ajax请求的数据始终相同

时间:2022-02-02 03:54:33

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

每当我使用jquery ajax请求内容时,即使我用服务器端代码替换,也会得到相同的数据

echo('hello, world!');

Used configuration:

使用配置:

  • SuSE Linux
  • SuSE Linux
  • Apache Server
  • Apache服务器
  • PHP 5
  • PHP 5

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.

正如您所看到的,我已经实现了两个代码来阻止javascript中的浏览器缓存。问题肯定在服务器端。

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.

如果你想看看 - 我正在测试的页面(限时在线)...在更新列表下方搜索“显示更多”按钮。这是触发ajax调用的地方。

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:

我解决了这个问题我的脚本出错了。没有必要手动附加时间戳,就像我上面所做的那样。 jQuery选项缓存:false就足够了。所以,它看起来像这样:

// 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();
    }
});

2 个解决方案

#1


1  

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

如果没有明确告知服务器,某些浏览器会非常积极地缓存AJAX请求。

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

尝试类似的东西(取自php手册,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

#2


0  

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,

#1


1  

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

如果没有明确告知服务器,某些浏览器会非常积极地缓存AJAX请求。

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

尝试类似的东西(取自php手册,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

#2


0  

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,