help.net


Musing on .Net

News

Windows Phone Apps

FotoBank


FotoIreland

Windows 8 Apps




hit counters






Open source CMS


My blog

Irish blogs

Locations of visitors to this page Get Chitika eMiniMalls

.Net useful

Blogs I read

PocketPC

SQL

Usability

[Ajax] Avoid the cache effect in Internet Explorer

This is a note for myself but it could be useful for anybody writing some Ajax calls.

If you use a GET method to send an asynchronous request to your server side code, if you don't do anything Internet Explorer will cache locally your request, so obviously you won't have the latest result in your response.

So instead of scripting something like this in Javascript (unless you use a POST method):

var url = 'GetCustomers.aspx?country=ireland'

use this:

var url = 'GetCustomers.aspx?country=ireland'  + "&dummy=" + new Date().getTime();

This will force IE to show a fresh version of your request all the time. Firefox doesn't seem to have this problem apparently.

 

Posted: Jun 08 2006, 03:49 PM by help.net | with 11 comment(s)
Filed under: ,

Comments

Tarun Jain said:

That is hack..
The proper way to do it is to add the following
to your page.

Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;

Tarun
-=-
# June 8, 2006 11:10 AM

Paschal said:

Tarun, thanks, tried that already and didn't work for me.
# June 8, 2006 11:24 AM

Buffalo said:

I too tried to force no-cache with no luck.

This is an excellent solution, imo.

Thanks!
# June 15, 2006 2:07 PM

John Bailon said:

Excellent hack, just what I was looking for. Thanks! :)
# July 2, 2006 6:26 PM

Thank You said:

thanks a lot..... its simply working great!
# July 15, 2006 6:32 AM

Jellybean said:

Firefox has this problem as well. If you load a page and then load some content, and then try to reload the very same content (assuming that this content has changed) - it will load the cached content. So if your site says: "Welcome to my page, You've been here 1 time" calling the page again, will display the same message: "Welcome to my page, You've been here 1 times" instead of recalling the page from the server to display: "Welcome to my page, You've been here 2 times" Your fix works nicely
# July 29, 2006 12:07 PM