简体   繁体   English

如何停止Internet Explorer缓存AJAX页面

[英]How to stop Internet Explorer caching AJAX pages

I'm not using JQuery or any other library. 我没有使用JQuery或任何其他库。 I simply have a PHP file which spits out a JSON result and my javascript acts accordingly. 我只是有一个PHP文件,会吐出JSON结果,而我的JavaScript也会相应地采取行动。

Unfortunatly, I've just spent an hour banging my head against a wall trying to figure out why I'm getting an 'undefined' for one of my variables where I KNOW I've set it in my php output. 不幸的是,我只花了一个小时就把头撞在墙上,试图弄清楚为什么我的变量之一会出现“未定义”的情况,我知道我已经在php输出中设置了它。

I finally figured it out: I went straight to the php page in IE and found that it had a cached result (ie, before I added that variable to the JSON string). 我终于弄清楚了:我直接进入IE中的php页面,发现它具有缓存的结果(即,在我将该变量添加到JSON字符串之前)。 As soon as I hit refresh, it got the new page and my code all suddenly started working. 当我点击刷新时,它进入了新页面,我的代码突然开始工作。

How can I force IE (and other browsers) not to cache my AJAX pages, since they're likely going to change very frequently anyway? 我该如何迫使IE(和其他浏览器)不缓存我的AJAX页面,因为它们可能随时都会频繁更改?

Add a timestamp or something random to your request 为您的请求添加时间戳或随机内容

or 要么

Use POST instead of GET 使用POST而不是GET

The usual way is to append a timestamp to the URL in question, eg 通常的方法是将时间戳添加到相关的URL,例如

var url += url.indexOf("?") == -1 ? "?" : "&";
url += "_=" + new Date().getTime();

适当地将expiraton标头设置为响应,或向请求添加随机或时间戳伪造参数。

Append the url with either Time, as suggested by others or use following: 根据其他人的建议,在URL后面附加时间或使用以下命令:

var rand = Math.floor(Math.random()*10000);

url = page.php?v=var&r=rand;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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