Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
|
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2006, 12:59 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XMLHTTPRequest returning stale file from the cache

I have made a small stock ticker style extension for firefox, which uses XMLHTTPRequest to download a csv file from my server. This csv has the latest market value, which I display on the user interface.

My problem is that fierfox is caching it! My script is fetching the url based csv file every hour, and the server indeed has updated file, but eh browser is returning the stale copy from its cache every time.

Please help.

Thanks.


 
Old June 1st, 2006, 06:59 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Insert cache control HTTP response headers.

Here's what a PHP version would look like:
Code:
<?php

    header('Pragma: public');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Cache-Control: must-revalidate');

    // etc
?>
Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old June 5th, 2006, 03:50 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One way to deal with this is to make sure each request has a unique member. For example, if you are using a GET, you can add a p
parameter to hold a randomly generated value:
(Assuming we have a method named statChanged to handle the Ajax callback)

Code:
var xmlHttp
var url="MyAjaxHandler.php?r=" + Math.random() + "&search=" + document.getElementById("txtSearch").value
xmlHttp=GetXmlHttpObject(stateChanged)
xmlHttp.open("GET", url, true)
The url variable now includes a parameter named "r" that will hold a random value. This makes each request unique, thereby averting the caching problem. The "r" parameter isn't used in the code handling this call - its only purpose is to make the request unique.

Make sense?

Woody Z http://www.learntoprogramnow.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
XMLHttpRequest.open smys123 Javascript 4 August 21st, 2007 02:18 AM
using XmlHttpRequest from .pac file sergeyk BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 September 5th, 2006 12:19 PM
xmlhttprequest - mozilla alexena XML 0 August 23rd, 2006 07:16 AM
XMLHttpRequest asyncronous by default? AGS BOOK: Professional Ajax ISBN: 978-0-471-77778-6 2 June 17th, 2006 04:44 PM
Cache dependency on a web file madkaikar_ashish General .NET 0 February 16th, 2005 12:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.