如何阻止IE缓存我的XHR

时间:2014-07-09 05:38:39

标签: javascript php ajax

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

如果我在chrome(最新版本)中运行上述代码,请点击div警报a

如果在不刷新我的窗口的情况下编辑php文件并将echo 'a'更改为echo 'b',然后保存并点击div,它会提醒b

以上是预期的行为。在最新版本的Internet Explorer中,由于某种原因,它始终会警告a

为什么请?

1 个答案:

答案 0 :(得分:0)

为了防止缓存问题(通常响应标头应该在某种程度上缓解这种情况),您最好自己破坏缓存:

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

xhr.open("GET", getUniqueURL(url), true);
相关问题