JSON and the Great Uncaching

Internet Explorer has this annoying little habit of caching AJAX calls.  This recently came out to bite me on a site I was working on which used AJAX to fill some drop-down boxes on an edit page.  The first time through was fine (eg. editing an existing item).  If I then created a new item, lo and behold the drop-down boxes were already selected on the previous item’s value!

This problem only occurs in IE, but there’s an easy solution.  Write a quck javascript function as follows:

function uncache(url) {
    var d = new Date();
    var time = d.getTime();
    return url + ((url.indexOf('?') == -1) ? '?' : '&') + 'time=' + time;

Then, whenever you make a parameterless AJAX call (eg. to /GetValues/), run it through the above function instead (eg. uncache(‘/GetValues/’) ).


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s