【问题标题】:No-cache and updating doesn't work in IE 10 with JSP and Javascript无缓存和更新在带有 JSP 和 Javascript 的 IE 10 中不起作用
【发布时间】:2014-04-26 11:55:33
【问题描述】:

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

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

我在 jsp 顶部附近使用 Cache-Control、Pragma 和 Expires 标头关闭缓存。

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

代码如下:

<%@ 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>

有什么线索吗?

【问题讨论】:

    标签: javascript html jsp internet-explorer caching


    【解决方案1】:

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

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

    【讨论】:

    • 这些参数会不会导致其他浏览器出现问题,还是只需要应用于IE?
    • 它们不会在其他浏览器中引起问题。
    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    相关资源
    最近更新 更多