无缓存和更新在使用JSP和Javascript的IE 10中不起作用

No-cache and updating doesn't work in IE 10 with JSP and Javascript

本文关键字:IE 不起作用 Javascript 更新 缓存 JSP      更新时间:2023-09-26

我已经关闭了缓存,并使用javascript从jsp页面中的rest调用手动检索状态。每个人都喜欢它(Chrome, FireFox, Safari, Opera),当然除了IE 8-11。

大部分工作是由getStatus()完成的。它使用XMLHttpRequest从REST调用中检索一组json状态。它解析json并将状态复制到td标签的innerHTML中。我使用setInterval()每5秒执行一次。

我关闭缓存控制,Pragma和Expires头,靠近jsp的顶部的缓存。

症状是状态永远不会更新。

代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
//tell browsers and proxy servers not to cache this page
if ("HTTP/1.1".equals(request.getProtocol()))
{
    response.setHeader("Cache-Control", "no-cache");
}
response.setHeader("Pragma","no-cache" );
response.setDateHeader("Expires", 0);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Application Title</title>
<link rel="stylesheet" type="text/css" href="<c:out value="${pageContext.request.contextPath}"/>/css/Admin.css" />
<script type="text/javascript">
    // need enough time for jbehave to parse the page
    var REFRESH = 5000; // 5 seconds
        // Update status after 5 seconds has elapsed.
    setInterval("jobStatus();", REFRESH);
    function jobStatus() {
        getStatus();
    }
    function getStatus() {
        var xhttpRequest = getXMLHttpRequest();
        xhttpRequest.open("GET", "<c:out value="${pageContext.request.contextPath}"/>/admin/resyncPhs/getstatus",  true);
        xhttpRequest.onreadystatechange = function() {
            if (xhttpRequest.readyState === 4) {
                if (xhttpRequest.status === 200) {
                     var json = xhttpRequest.responseText;
                     var parsed = JSON.parse(json);
                     document.getElementById("status").innerHTML = parsed.status;
                     document.getElementById("progress").innerHTML = parsed.progress;
                }
            }
        };
        xhttpRequest.send(null);
    }
    function getXMLHttpRequest() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
</script>
</head>
<body onLoad="jobStatus();">
                <table class="resyncBodyTable">
                    <tr>
                        <td />
                        <td>Labels:</td>
                        <td id="status">${status}</td>
                        <td id="progress">${progress}</td>
                    </tr>
                </table>
</body>
</html>

什么线索吗?

IE比其他浏览器更需要说服:

response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma","no-cache");