简体   繁体   English

XMLHttpRequest不会在javascript的间隔中更新

[英]XMLHttpRequest doesn't update in javascript's interval

I use AJAX to check user updates every 2 seconds, but my javascript does not update the response. 我使用AJAX每2秒检查一次用户更新,但是我的JavaScript不会更新响应。

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. 我有一个带有XMLHttpRequest对象的javascript文件,它每2秒将请求发送到另一个文件(.php),并在其中获取具有更新的XML。 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): 我的javascript文件包含以下代码(简体):

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: 然后,我得到了PHP文件( check_for_notifications.php ),其中包含以下代码:

$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 ( eg I change the date to '1.1.2019 11:00' and save it ) I still receive the '12:00' in the console. 我每两秒钟会在控制台中收到一个日志,但是当我更改PHP文件时,尽管间隔正在进行( 例如,将日期更改为'1.1.2019 11:00'并保存 ),但我仍然收到'12 :00”。 For me, it seems that it doesn't update and it still has the repsonseXML cached. 对我来说,它似乎没有更新,并且仍然保留了repsonseXML Is there any way I could "flush" the output or am I doing it wrong? 有什么办法可以“刷新”输出,还是我做错了?

It's probably a cache problem. 这可能是缓存问题。 In the network browser console, you should see a response of type 304 Not modified. 在网络浏览器控制台中,您应该看到类型304未修改的响应。

To be sure, you can add an element in the url to bypass the cache: 可以肯定的是,您可以在url中添加一个元素来绕过缓存:

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. 如果可行,则需要配置服务器(apache或nginx)以防止文件被缓存。 It's cleaner than the timestamp solution. 它比时间戳解决方案干净。 the browser does not store cached files unnecessarily. 浏览器不会不必要地存储缓存的文件。

Apache .htaccess or Apache conf, something like Apache .htaccess或Apache conf,类似

<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 Nginx conf,类似

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/ 您还可以看到获取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. 请注意您的代码,它可以同时启动多个请求,如果用户过多,则可以使用DOS。

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.... 如果由于服务器速度太慢而导致请求时间超过2秒,那么与此同时,其他请求将被发送,这将使服务器速度进一步降低....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM