Current location: Hot Scripts Forums » General Web Coding » JavaScript » [SOLVED] AJAX Cache


[SOLVED] AJAX Cache

Reply
  #1 (permalink)  
Old 06-16-08, 09:06 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Talking [SOLVED] AJAX Cache

Hi everyone, hope you all had a good weekend.

Ive ran into a slight snag with Ajax. I import content provided by a PHP script which opperates from a few form fields on my xhtml page which calls the ajax function on a click of an image. The problem is that it seems that the part of the content that is refreshed by ajax is being ignored by FF2 and it shows the old information from my DB. A refresh of the page clears the cache and shows the new information. Since the idea of Ajax is that it is supposed to work without the entire page loading in again from a forced refresh.

After trying to id the problem through google I see nothing but information about how browsers dont support the caching of Ajax. Since most of the stuff on the web is out-of-date I thought Id check to see where this problem may lie. I dont include any headers in my HttpRequest so its a straight forward GET although I actually first call a function with a POST method to grab the form info the last function works by GET.

I must mention that this is very unpredictable with it working sometimes and not working others (mostly) which is why I went looking for a cache solution.

Can anyone provide me the headers for Ajax to turn of cache (to support all browsers would be good) ie.
Code:
httpRequest.setRequestHeader("Cache-Control", "Don't Bloody Cache It!");


Thanks
Dal
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-16-08, 09:10 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
I'm not sure about headers, but I always do it this way:
javascript Code:
  1. var url = 'ajax.php?r=' + Math.floor(Math.random() * 99999);
  2. httpRequest.open('GET', url, true);
  3. // ...
  4.  
... It's just a dummy variable which is being sent. This way it always re-fetches the content instead of using the cache.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-16-08, 09:31 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
I used that technique when I had a picture upload problem. Never occured to me to use it in this case. Since I know it works extremely well to combat against the image cache I know it will work just as good with the ajax request. Thanks Nico!

Im going to close this thread (which reminds me I need to close the other one I started yesterday ) but if anyone has them headers for cache control for future reference, that would be handy.

Thanks
Dal.
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-16-08, 09:36 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Hmm - Still showing old material. Content Cache Headers would be really good! My original question still stands.

OOPS - Probably just me, I didnt give it a chance. Sorry Thread Closed again. Rand function added to ALL Ajax Get function calls.
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."

Last edited by DAL; 06-16-08 at 09:39 AM. Reason: Impatience
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-16-08, 10:23 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
if anyone has them headers for cache control for future reference, that would be handy.
I'm not sure if it's actually possible to tell the browser to "not cache" the data, using headers from the client's side. The headers you send in your script are the headers that are being sent to the server, and not the other way around.

You can try to send "no cache" headers from your PHP script to your AJAX script, but I've tried it a couple of times and I was never successful with it. (Mainly because of Internet Explorer). So I always sticked to the method above.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-16-08, 11:27 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Ok Cool.

Thanks
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-16-08, 12:08 PM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
When you send a request to the server, the server won't cache your request. Caching only happens on the client side, like Nico said. You'll need PHP to send the no-cache headers (cache-control & pragma)
__________________
Jack Bauer makes Chuck Norris cry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-17-08, 10:15 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You can also switch to POST from GET, unless it's too much work on the server.
POST requests are not cached by the browser since they are most likely outdated if resent.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NCache 3.1 provides a smarter cache for Enterprise Library 2.0 smars General Advertisements 0 02-05-07 08:29 AM
AJAX problem with php dynamic url scott2500uk PHP 3 10-28-06 11:33 AM
[announce] NCache provides Distributed Caching for Enterprise Library 2.0! Kathie ASP.NET 0 07-19-06 06:37 AM
Cache Callback Problem - it doesn't happen titch_stewart ASP.NET 3 12-30-04 05:18 PM


All times are GMT -5. The time now is 12:54 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.