Was working on this really simple website at my job the other day. Since it was so simple, I decided to make the “data” area of the site a simple XML file that I’d just update and re-push out to the site to update data behind listboxes, etc. No big deal.
Or so I thought.
Turns out the following:
if (window.XMLHttpRequest) { requestor = new XMLHttpRequest(); } else { requestor = new ActiveXObject("Microsoft.XMLHTTP"); } requestor. requestor.open("GET", "data/Pricing.xml", false); requestor.send(); pricingXml = requestor.responseXML;
Is good until you change Pricing.xml. Since the request has been made once before recently, the server’s cached the value of the response (the XML file data) and gives back to you what it cached, NOT what you updated.
Grr.
So I did some Binging and came back w/ a pretty ingenious way of getting around this:
requestor.open("GET", "data/Pricing.xml?" + new Date().getTime(), false); // getTime used to get unique request, so it's not cached by the server requestor.send(); pricingXml = requestor.responseXML;
See what I did there? It makes the URL that’s requested be unique every second, but still requests the Pricing.xml file so the data’s not changing. Just slap on an ever-changing URL parameter (that’s never used) and you’re golden. Pretty sweet.
I also found a couple of other things that you *should* do to implement this “correctly,” but from what I’ve seen, the above method works just fine. That is, I haven’t found an instance where I’m getting back cached data.
Yet.