Page 1 of 1

ajax update problem

Posted: Wed Mar 14, 2007 12:07 pm
by murlopaz
I am trying to retrieve temperature data from a txt file on the serve + update it every 3 seconds.

I do get the updates, but the value is the same even when I go and change the temperature value manually from the txt file.

What I mean is that, looks like ie chaches the temperature value or something.
can anybody help?

Code: Select all

<?
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Time</title>
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest()
{
    if (window.ActiveXObject)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            xmlHttp = new XMLHttpRequest();
        }
}

function startRequest()
{
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "outdoortemp.txt",true);
    xmlHttp.setRequestHeader("Cache-control","no-chache");
    xmlHttp.setRequestHeader("Pragma","no-chache");
    xmlHttp.send(null);
}

function handleStateChange()
{
        if (xmlHttp.readyState == 4)
        {
                if (xmlHttp.status == 200)
                {
                    //document.write(xmlHttp.responseText);
                    alert(xmlHttp.responseText);
                    //setInterval('document.write(xmlHttp.responseText)',3000);
                }
        }
}

</script>

</head>

<body>

<script type="text/javascript">
//startRequest();
//setInterval('startRequest()',3000);
//1000 milliseconds=1 sec
</script>
<form action="#">
<input type="button" value="Temperature" onclick="startRequest();"/>
</form>
</body>
</html>
The Ninja Space Goat | Just use PHP bbcode tags when there's php in the html, otherwise, use the syntax drop-down menu (select HTML)

Posted: Wed Mar 14, 2007 12:13 pm
by feyd
The request headers you are sending aren't actually request headers, they are response headers (with bad values, I may add) that are supposed to be sent by the server.

Posted: Wed Mar 14, 2007 12:18 pm
by murlopaz
so what's the problem?
I am pulling the temperature file from a text file (only the temp value is in there).
Why isn't the new value shown when I manually update the temperature file?

Thanks

Posted: Wed Mar 14, 2007 12:30 pm
by feyd
Caching?

Posted: Wed Mar 14, 2007 12:31 pm
by murlopaz
got it.
Thanks

Posted: Thu Mar 15, 2007 11:02 am
by murlopaz
This works fine in firefox, but doesn't update the temp value in internet explorer. Does anybody have any ideas why?

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Time</title>
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest()
{
    if (window.ActiveXObject)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            xmlHttp = new XMLHttpRequest();
        }
}

function startRequest()
{
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "KDMH.xml",true);
    xmlHttp.send(null);
}

function handleStateChange()
{
        if (xmlHttp.readyState == 4)
        {
                if (xmlHttp.status == 200)
                {
                    /*** temp is the name of the div container ***/
                    var temp_xml=xmlHttp.responseXML;
                    var temperature=temp_xml.getElementsByTagName("temp_f")[0];//.childNodes[0].nodeValue;
                    //document.getElementById("temp").innerHTML=temperature.childNodes[0].nodeValue;
                    alert(temperature.childNodes[0].nodeValue);
                }
        }
}

</script>

</head>

<body>
<table>
<tr>
<td id=temp>
<script type="text/javascript">
startRequest();
setInterval('startRequest()',3000);
//1000 milliseconds=1 sec, should be 15 minutes = 900000
</script>
</td>
</tr>
</table>
</body>
</html>