Wednesday, April 7, 2010

Ajax Tips - Avoid the cache effect in IE

Let's say you use a GET method to send an asynchronous request to 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 = "getCustomer.do?cm=S001"

use this:

var url = "getCustomer.do?cm=S001" + "&nocache="+ new Date().getTime();

This will force IE to show a fresh version of your request all the time because this request will put in a constantly changing variable in the URL such as date with seconds in Internet Explorer.

or

var url = "getCustomer.do?cm=S001" + "&random="+math.random()

Firefox doesn't seem to have this problem apparently.

We adopt jquery tech to get JSON response from server side, but only GET method is provided by this method $.getJSON(),

So we have to do more things to resolve IE cache issue as follows.

$.getJSON("../ajax/enquirecommcode.do", {"cmCode" : cmCode,"cpcmCode":cpcmCode,"nocache":Date()}

$.getJSON("../ajax/enquireoutrightleg.do",{"commCode" : commCode,"nocache":Date()}

No comments:

Post a Comment