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

XMLHttpRequest不会在javascript的时间间隔内更新

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){
        console.log(updates.responseXML);
    }
};

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

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');
print($response->asXML());

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
        </IfModule>
    </Files>
    

    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....

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

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图