Ozzah
Ozzah

Reputation: 10701

How to stop Internet Explorer caching AJAX pages

I'm not using JQuery or any other library. I simply have a PHP file which spits out a JSON result and my javascript acts accordingly.

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.

I finally figured it out: I went straight to the php page in IE and found that it had a cached result (i.e., before I added that variable to the JSON string). 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?

Upvotes: 1

Views: 1258

Answers (4)

achzodiac
achzodiac

Reputation: 1

Append the url with either Time, as suggested by others or use following:

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

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

Upvotes: 0

Phil
Phil

Reputation: 164760

The usual way is to append a timestamp to the URL in question, eg

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

Upvotes: 3

nakhli
nakhli

Reputation: 4059

Set the expiraton header proprerly to the response or add a random or timestamp fake parameter to the request.

Upvotes: 2

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

Add a timestamp or something random to your request

or

Use POST instead of GET

Upvotes: 4

Related Questions