现在的位置: 首页 > 综合 > 正文

防止js加载缓存结果

2013年04月21日 ⁄ 综合 ⁄ 共 815字 ⁄ 字号 评论关闭

问题描述:

1.jsp:

<%@ page contentType="text/html; charset=gbk" pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script src="/test/js/jquery-1.6.js"></script>
<script>
$(function(){
	$("#button").click(function load(){
		$("#num").load("2.jsp");
	});
});
</script>
</head>
<body>
<input id="button" type="button" value="load"/>
<div id = "num"> 
<div>
</body>
</html>

2.jsp

12345

文件目录为:

WebContent
-web
--1.jsp
--2.jsp

点击1.jsp页面的load按钮后,会显示2.jsp页面中的"12345",然后删除页面2.jsp,刷新1.jsp页面,点击load按钮,依然会显示12345

 

问题分析:

服务器上的2.jsp页面已被删除,如果向服务器发送请求,将找不到2.jsp页面,也就不会显示12345

说明此时并没有向服务器发送请求,而是直接返回了保存在缓存中的上次请求结果。


 

解决方式:

1,请求url后加上请求时间或者Math.random()(即保证每次的请求都是不同的,这样就不会返回保存在缓存中的结果,而是每次独立的向服务器发送请求)

$("#num").load("2.jsp?math="+Math.random());

 

抱歉!评论已关闭.