
06-16-08, 09:06 AM
|
 |
Code Master
|
|
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
[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.
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."
|

06-16-08, 09:10 AM
|
 |
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:
var url = 'ajax.php?r=' + Math.floor(Math.random() * 99999); httpRequest.open('GET', url, true); // ...
... It's just a dummy variable which is being sent. This way it always re-fetches the content instead of using the cache.
|

06-16-08, 09:31 AM
|
 |
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."
|

06-16-08, 09:36 AM
|
 |
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
|

06-16-08, 10:23 AM
|
 |
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.
|

06-16-08, 11:27 AM
|
 |
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."
|

06-16-08, 12:08 PM
|
 |
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
|

06-17-08, 10:15 AM
|
|
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.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|