Forum Moderators: open

Message Too Old, No Replies

XMLHttpRequest does not get new data on refresh

XMLHttpRequest problem

         

greenowl

9:08 pm on Mar 18, 2011 (gmt 0)

10+ Year Member



Hi there

I have been using XMLHttpRequest object to get a file from my localhost server. If i change the file at the server and refresh the webpage containing the javascript request the contents of XMLHttpRequest.responseText does not change. If i open a new tab to access the website then the new version of the file is used.

Can anyone explain why and what i can do about it?

I am running an Apache server on my PC and my browser is ie8

Many thanks

Demaestro

11:04 pm on Mar 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sounds like a caching issue.

A simple workaround to this is when you request the file add a query string arg to the URL that has a random value that will be different each page load... effectively tricking the browser into thinking it is a different file each time which makes it not use a cached version.

http://example.com/myFile.xml?hash=oierutoer

You can write a little function that returns a random string or use a timestamp, which will be unique each page load.

http://example.com/myFile.xml?hash=201103181245678

greenowl

9:42 pm on Mar 19, 2011 (gmt 0)

10+ Year Member



Thank you Demaestro

My code line requesting the file is:
xmlhttp.open("GET","gallery2/gallery2c.php?id=Math.random()",true);
and i think this is much the same as your suggestion. Indeed it would not work at all on a refresh until i added this, courtesy of w3schools, presumably due to the cache issue. I will try the timestamp, just in case the random isn't random on a page refresh.

Any other ideas - are there any ie browser setting. I had a similar problem with java applets - to get the new version to load you had to completely close the browser - at least this time a new tab does the trick!

TheMadScientist

12:05 am on Mar 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're requesting the same thing every time if this is really your line of code:
xmlhttp.open("GET","gallery2/gallery2c.php?id=Math.random()",true);

xmlhttp.open("GET","gallery2/gallery2c.php?id="+Math.random(),true);

greenowl

5:06 pm on Mar 20, 2011 (gmt 0)

10+ Year Member



Yes that was my code! I was sending the string and not the return value of the function so DeMaestro was quite right it was the cache problem.

Your help much appreciated.