Microsoft Internet Explorer caches Ajax calls

Problem

This one came to be as such a big surprise that I had to Google it several times to make sure it is not my own mistake.

A little bit of background for those who are not web developers: traditionally the contents of the HTML pages did not change once loaded into the browser. Ajax is a technology that allows Javascript to load small pieces of data from the server and change the HTML page without re-loading it. Ajax is a very common technique that is used in most web sites from Facebook to GMail and even mobile web sites. In simple language, it is something similar to RPC (remote procedural calls in C) or RMI (remote method invocation in Java).

Today I was debugging one of my latest web applications. It has some controls that allows the user to set some parameters on the server:

GUI of my web application uses Ajax

It works in every browser except IE. Tracing the network packets, I realized that setting a parameter works. It is getting the parameter that has a problem. Every time the interface asked the server for the latest status, it returned an old value (ie. the value before the parameter was set). One of the craziest guesses I had was that this old value is coming from some sort of cache! And it turned out to be true! It means every time Javascript wants to update the page, Internet Explorer fools it by returning a cached result instead of actually sending the request to the server. As a result, the page will be “updated” with old data.

See what other developers wrote about it:

I was tearing my hair out trying to work out the cause of this problem… (Jeremy Epstein)

Strictly speacking IE caches Ajax GET requests, therefore making all your web2.0 tricks effortless! (Daniel Vecchiato)

This can be a nightmare to deal with, as you cannot ask all users of a web site to change their browser settings. (Dan Nye)

I’ve never encountered a case where I want ajax to NOT contact the server. (Dean Moses)

Since it is a really frustrating “design feature” of Microsoft IE, I decided to write about it here. This aggressive solution apparently tries to improve the speed of Internet Explorer but it doesn’t help the infamously slow browser. In fact this solution to speed up a browser is like putting two virtual reality LCDs instead of a car’s windshield and simulate a fast moving car by showing movies of driving in fast road. The car’s engine is so weak that it cannot even drive in standard roads! Microsoft should realize that in the world of free speedy real cars, users would hardly choose a virtual reality one, even if it comes pre-installed in their garage when they buy a home!

Solution

The best solution for Internet Explorer team is to follow web standards. If you cannot interpret standards, cheat! Just check how open source browsers like Firefox implement it and follow their lead. It’s the only case where cheating hurts less than not cheating!

Various web developers have suggested workarounds for the issue. Check out the links of the quotes above. There are mainly 3 solutions:

  • add a timestamp as an extra parameter to every request so that IE thinks it is a different request and doesn’t load it from cache. Of course the server ignores this parameter.
  • use “POST” requests instead of “GET”. Apparently IE just caches GET requests.
  • the server response can add a header to the response that explicitly asks the browser not to cache it.
  • for debugging purpose it is possible to ask IE to behave “normally”:

Telling IE to behave like other browsers and not to cache Ajax

Advertisements

2 thoughts on “Microsoft Internet Explorer caches Ajax calls

  1. Which version of IE is this? What about IE11? Also, the F12 developer tools have been “reimagined” now so what’s the fix?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s