PDA

View Full Version : Begginer Ajax Question


jayR
06-29-2006, 09:57 AM
I've done a little bit with ajax, but hardly enough to really be comfortable with it. I have a simple script that is as follows:

<script>
var xmlhttp
function getInfo() {
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open('GET', 'data.php', true);

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("info").innerHTML = (xmlhttp.responseText);
}
};
xmlhttp.send(null);
}
setInterval("getInfo()",2000);
</script>

then farther down the document I have the div tag with the id=info and data.php just reads in a txt document that I've been manually changing to test with. The problem is that this works fine in Firefox but (as happens all to often) it won't work in IE. IE will updata it the first time through but after that it won't pick up any of my changes. I can't figure out what I did wrong, so I'm hoping some people here that are much wiser then I could see my mistake and point me in the right direction. Thanks.

I did notice that if I go into Tools, Internet Options, and Delete Files then it will get the next update. But then it will stop updating again. Its very frusterating.

RysChwith
06-29-2006, 05:27 PM
Sounds like you're having a caching issue.

A somewhat clunky solution would be to add a query string to the URL, so that IE thinks it's a new page every time (randomly generated number or some such).

There's probably a better way, though.

Rys

Jon Hanlon
06-29-2006, 07:22 PM
Yes, it is a caching problem.
You can add a cache-control header to your request, but the easiest thing to do is pass a unique id back with the time in it.
"&uid=" + (new Date()).getTime()

in your case:
xmlhttp.open('get', 'data.php?uid=' + (new Date()).getTime(), true);