dsfb20227 2019-01-18 14:19 采纳率: 0%
浏览 94


I use AJAX to check user updates every 2 seconds, but my javascript does not update the response.

I have one javascript file with XMLHttpRequest object and every 2 seconds it sends a request to another file (.php) where it gets XML with updates. For some reason, it doesn't always get the newest content and seems to have some old cached.

My javascript file contains this code (simplified):

var updates = new XMLHttpRequest();
updates.onreadystatechange = function(){
    "use strict";
    if(updates.readyState === 4 && updates.status === 200){

var timer = 0;
timer = setInterval(function(){
    "use strict";
    updates.open('GET','scripts/check_for_notifications.php', true);

Then I have the PHP file (check_for_notifications.php), where I have this code:

$response = new SimpleXMLElement('<xml/>');

$update = $response->addChild('update');
$update->addChild('content', 'New message');
$update->addChild('redirect', 'some link');
$update->addChild('date', '1.1.2019 12:00');

header('Content-type: text/xml');

Every two second I receive a log in my console, but when I change the PHP file, while the interval is in progress (e.g. I change the date to '1.1.2019 11:00' and save it) I still receive the '12:00' in the console. For me, it seems that it doesn't update and it still has the repsonseXML cached. Is there any way I could "flush" the output or am I doing it wrong?

  • 写回答

1条回答 默认 最新

  • douchun6221 2019-01-18 14:46

    It's probably a cache problem. In the network browser console, you should see a response of type 304 Not modified.

    To be sure, you can add an element in the url to bypass the cache:

    updates.open('GET','scripts/check_for_notifications.php&nocache=' + new Date().getTime(), true);

    If this works, you will need to configure the server (apache or nginx) to prevent the file from being cached. It's cleaner than the timestamp solution. the browser does not store cached files unnecessarily.

    Apache .htaccess or Apache conf, something like

    <Files "check.php">
        <IfModule mod_headers.c>
            Header set Cache-Control "no-cache, no-store, must-revalidate"
            Header set Pragma "no-cache"
            Header set Expires 0

    Nginx conf, something like

    location = /check.php {
        add_header 'Cache-Control' 'no-cache, no-store, must-revalidate';
        expires off;

    You can also see the fetch api : https://hacks.mozilla.org/2016/03/referrer-and-cache-control-apis-for-fetch/

    Be careful with your code, it can launch several requests simultaneously and find yourself with a DOS if there are too many users.

    If requests take more than 2 seconds because the server is slow, others requests will be sent in the meantime, which will slow down the server even more....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应