简体   繁体   English

如何阻止IE缓存XHR

[英]How to stop IE from caching my XHR

<?php
    if(isset($_GET['dt']))
    {
        echo 'a';
        die();
    }
?>
<div onclick="call('data_call.php?dt=a')">DATA</div>
<script>
function call(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onload = function (e) {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                alert(xhr.responseText);
            } else {
                alert("");
            }
        }
    };
    xhr.onerror = function (e) {
        alert("");
    };
    xhr.send(null);
}
</script>

If I run the above code in chrome (the latest version) and click the div it alerts a 如果我运行铬(最新版本)上面的代码,然后单击div它提醒a

If, without refreshing my window I edit the php file and change the echo 'a' to echo 'b' then save it and click the div, it alerts b . 如果在不刷新我的窗口的情况下,我编辑了php文件,并将echo 'a'更改为echo 'a' echo 'b'然后将其保存并单击div,它将发出b警报。

The above is the expected behaivor. 以上是预期的行为。 In the lastest version of internet explorer, for some reason it always alerts a . 在Internet Explorer的最新版本,由于某种原因,它总是提醒a

Why please? 告诉我为什么?

To prevent caching issues (typically response headers should mitigate this to a certain degree), you're better off busting the cache yourself: 为了防止出现缓存问题(通常,响应标头应在一定程度上缓解这种情况),最好自己清除缓存:

function getUniqueURL(url)
{
    return url + (url.indexOf('?') == -1 ? '?' : '&') + '_=' + new Date().getTime();
}

xhr.open("GET", getUniqueURL(url), true);

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

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