jQuery AJAX get() function cached in IE

From the 'how the heck were we supposed to know this?' department, a little tidbit I'd like to share, perhaps saving some poor, frustrated developer from the same type of mystifying madness:

jQuery "get()" operations are cached in IE unless you pass in a unique URL !

By cached, I mean, you can have a remote page with a query or other function, which responds to a unique set of values being passed in, and it will work fine in Firefox, but in IE, you'll get the same results from the remote page, even if the response is unique (like query results that have changed, for the same record), unless the values you are passing in via get() are also unique (i.e. you haven't looked up that particular record yet).

So, before you go banging your head on the floor and screaming out 'why oh why can't i find the problem, let alone the solution?!?' (not that i would ever do that), read this post, which i found via a quick Google search for 'jquery get IE cache' : http://www.sitecrafting.com/blog/ajax-ie-caching-issues/ 

I only had the presence of mind to run that search once i got my ajax results to display as an alert on the calling page, and deduced that the displayed content was based on the first values passed in, while all other updates and requests were ignored.

The fix, as that post suggests, is to create a unique url. In this case, i use javascript to append a random number to my function, - in this case, I just use javascript to create a random number right inline, then add that as a meaningles url variable to the page in the get() function:

                           // set up unique url
                            var randomNo = Math.floor(Math.random()*9999999);
                            var urlStr = 'journalLookup.cfm?r=' + randomNo;
                            // run get function using randomized URL
                            $.get(urlStr,{jdate:dateLookup}, function(data){  .... [ function here ]

If this saves you some aggravation, let me know in the comments ... maybe i'll feel better about the time i just wasted chasing this mystery 'feature' in IE!!

 

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Or you can use $.ajax with cache argument set to false - jQuery will append that random string thing for you (not sure if .get has a cache argument... ).
# Author Felix Tjandrawibawa | 5/31/10 6:14 AM
Would using something like ajaxStart give you a way to do it automatically?

http://api.jquery.com/ajaxStart/
# Author Raymond Camden | 5/31/10 10:18 AM
@Ray ~ looks like ajaxStart is aimed at preventing ajax from firing while other ajax requests are running - this isn't a concurrent-query thing (which i could understand) but IE actually caching the results it gets from your ajax page (along with the variables posted) like it would cache an image or css file ~ I still can't see any good reason for that, given that ajax is generally used to get dynamic info.

@Felix ~ i looked for a cache argument on $.get(), but, nope. Didn't think of rephrasing it to use $.ajax - willl consider that next time, thanks
( .ajax() reference: http://api.jquery.com/jQuery.ajax/ )
# Author Michael Evangelista | 5/31/10 11:12 AM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista