---
title: "解决jQuery1.1 Ajax请求时cache的问题"
author: "wangcheng"
date: "2007-10-27T15:56:09Z"
tags: ["Ajax", "Cache", "HTML", "JSP", "jQuery"]
slug: "jquery1-1-ajax-cache"
draft: false
lastmod: "2007-10-27T15:56:09Z"
id: 135887
url: "https://wangcheng.iteye.com/blog/135887"
---

项目里使用jQuery 1.1.3.1版本，遇到的问题是jQuery缓存ajax请求的数据，在jsp和servlet里设置了参数，但还是被jQuery缓存。

java 代码

 

1. response.setHeader("Cache-Control","no-store");
2. response.setHeader("Pragma","no-cache");
3. response.setDateHeader("Expires", 0);

html 代码

 

1. &lt;meta http-equiv="Cache-Control" content="no-store"/&gt;
2. &lt;meta http-equiv="Pragma" content="no-cache"/&gt;
3. &lt;meta http-equiv="Expires" content="0"/&gt;

使用上面的参数，也不是完全起作用。

在jquery.com看到其1.2版本的ajax方法添加了cache选项，可以解决缓存问题。

js 代码

 

1. $.ajax({
2. url: "test.html",
3. cache: false,
4. success: function(html){
5. $("#results").append(html);
6. }
7. });

试用后感觉不错。就准备用1.2版本替换现在的1.1.3，但是发现项目中基于jquery的plugin很多都还是基于jquery 1.1版本，而且到现在还没有放出支持1.2的新版本。自己试着替换了一下，果然出错了。即使用了jQuery 1.1 Compatibility Plugin也不是所有插件都好用。 只好放弃使用1.2的念头。随即开始翻看1.2的代码，发现其ajax cache选项解决缓存的方法非常简单，摘录如下

js 代码

 

1. if ( s.cache === false && s.type.toLowerCase() == "get" )
2. s.data = (s.data ? s.data + "&" : "") + "\_=" + (new Date()).getTime();

太好了，我只要同样也在jquery 1.1版本的ajax方法的data上也加个时间戳就行了。使用如下方式解决问题

js 代码

 

1. $.ajax({ url: url,
2. type:"get",
3. dataType:"html",
4. data: "\_=" + (new Date()).getTime(),
5. success:function(html){...}
6. });