简体   繁体   中英

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

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 .

The above is the expected behaivor. In the lastest version of internet explorer, for some reason it always alerts 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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