This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Question to authors:  AJAX caching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Question to authors:  AJAX caching" Watch "Question to authors:  AJAX caching" New topic
Author

Question to authors: AJAX caching

Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 226

I was playing with a very elementary AJAX example recently (user registration on a website) and I noticed that once a server-side asynchronous hit has been made for a particular "username" input-value, IE (I am not sure about other browsers) would not even make another HTTP Request for the same "username" input value next-time, it just returns the previous result. This led me to the belief that some amount of caching is involved here.

I am aware that the way out of this problem is probably the same as preventing browser-caching for any dynamic page (link for the un-inited), but I want to know where exactly does this caching happens, is it being done by the AJAX engine or is it similar to normal browser caching?

Thanks for your time and best wishes for the success of the book.


Debashish
SCJP2, SCWCD 1.4
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
I too thought that the browser-caching has some effects, but after setting the browser-caching effects like "expires, no cache etc.," I didnt had this problem.
I have a case where AJAX reads a dynamically generated xml file for every onChange event input. So far this is working fine in both IE and mozila.

I have another case where AJAX reads a static xml file on page load event.
If i change the xml file content, mozile shows it fine, but IE seems to read the old xml file for a while.


Spritle Software Blogs
Ryan Asleson
Author
Ranch Hand

Joined: Sep 15, 2005
Posts: 39
Yes, at times browsers will cache the request and not make a new request even though we may want it to. This is controlled by the browser itself and not the XMLHttpRequest object.

You're on the right track for avoiding it. The techniques I'm aware of are setting the headers to "no cache" and "expires" and also to append a unique timestamp to the end of every URL that's used in an Ajax request, as the unique URL will force the browser to perform a fresh request each time.


Ryan Asleson<br />Co-Author, <a href="http://www.amazon.com/Foundations-Ajax-Foundation-Ryan-Asleson/dp/1590595823/sr=1-1/qid=1158926537/ref=pd_bbs_1/002-7412154-2924860?ie=UTF8&s=books" target="_blank" rel="nofollow">Foundations of Ajax</a> and <a href="http://www.amazon.com/Pro-Ajax-Java-Frameworks/dp/1590596773/ref=sr_11_1/002-7412154-2924860?ie=UTF8" target="_blank" rel="nofollow">Pro Ajax and Java Frameworks</a><br /><a href="http://taconite.sf.net" target="_blank" rel="nofollow">Taconite</a> Lead Developer
Alan Walker
Greenhorn

Joined: Feb 15, 2004
Posts: 26
Am I right in thinking that another way to avoid this problem is to use POST rather than GET for sending your requests?

Since POST is designed to change the state on the server, the browser should send every request, even if the URL and data are exactly the same as a previous request.


Chihuahua - Ajax-powered online word puzzle, <a href="http://chi.lexigame.com" target="_blank" rel="nofollow">http://chi.lexigame.com</a> <br />Letterbox Word Game Online, <a href="http://letterbox.lexigame.com" target="_blank" rel="nofollow">http://letterbox.lexigame.com</a> <br />Lexi Word Game for Java-enabled phones, <a href="http://www.lexigame.com" target="_blank" rel="nofollow">http://www.lexigame.com</a>
RajatChopra
Greenhorn

Joined: Oct 31, 2005
Posts: 4
Hi,
I am facing a similar problem. I want to refresh the data on my page every few seconds. But the AJAX script fetches the same data every time. I have used the random function in my servlet. The servlet shows a random number each time, but somehow the JSP calling the servlet through AJAX, is unable to do so.

It keeps on displaying the same data again and again.

I have tried using response.setHeader(" ..."), but that prevents any data from being displayed on the JSP. I am using IE.

Please suggest some solution.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Use no cache headers or append a timestamp to the request. The timestamp will force IE to get new info.

Eric
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
on similar requirement I need to fetch tooltip only if it's changed from what it was displayed (cached) first time to avoid unnecessary server trip if content is not changed. any advise?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Stick it in a JavaScript array, if the array element does not exist then go get it. I use that all the time in my applications.

Eric
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
thanks eric. however I don't know whether I got your idea correctly or I didn't clearly mention my requirement.

I do following
-grab and display tooltip(AJAX call) on mouse hover on a particular item.
-you are saying, i should store this tooltip value in JS array?
-but even if I store first time fetched value in array, how do I know whether value on server has changed or not for this tooltip (data for tooltip is being feed by another application, which can alter tooltip data on the fly while first app is running and displaying tooltip).

any ideas?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question to authors: AJAX caching
 
Similar Threads
Caching is not working
How to getParameter value
Avoiding two sessions by a single user
Ajax and the Caching issue
Some Server Side Caching?