Coffeehouse
Coffeehouse

Reputation: 505

Ajax request on an XML file

I have a native AJAX request that inserts node Values into html divs. If I change the XML values and upload it to the server, while the website is running, Chrome and IE won't update the values (even Shift-F5 reloading doesn't help) and Firefox does it after some time (not after the 1000ms of the setInterval).

The AJAX script in the html file looks like this:

    <script>

    //----ANFANG AJAX REQUEST

    function loadXMLDoc()
    {
    var xmlhttp;
    var txt,x,i;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        xmlDoc=xmlhttp.responseXML;
        txt="";
        x=xmlDoc.getElementsByTagName("Value");
          /* 
          for (i=0;i<x.length;i++)
          {
          txt=txt + x[i].childNodes[0].nodeValue + "<br>";
          }
          */
        txt=x[0].childNodes[0].nodeValue;
        document.getElementById("Eingabe_Web").innerHTML=txt;    
        txt=x[1].childNodes[0].nodeValue;
        document.getElementById("differenzdruck").innerHTML=txt;
        txt=x[2].childNodes[0].nodeValue;
        document.getElementById("w1").innerHTML=txt;
        txt=x[3].childNodes[0].nodeValue;
        document.getElementById("w2").innerHTML=txt; 
        txt=x[4].childNodes[0].nodeValue;
        document.getElementById("w3").innerHTML=txt;
        txt=x[5].childNodes[0].nodeValue;
        document.getElementById("w4").innerHTML=txt;
        txt=x[6].childNodes[0].nodeValue;
        document.getElementById("w5").innerHTML=txt;
        txt=x[7].childNodes[0].nodeValue;
        document.getElementById("w6").innerHTML=txt;
        txt=x[8].childNodes[0].nodeValue;
        document.getElementById("w7").innerHTML=txt;
        txt=x[9].childNodes[0].nodeValue;
        document.getElementById("w8").innerHTML=txt;    
        txt=x[10].childNodes[0].nodeValue;
        document.getElementById("w9").innerHTML=txt;
        }
      }
    xmlhttp.open("GET","einlesen.xml",true);
    xmlhttp.send();
    document.getElementById("refresh").innerHTML = new Date().getTime();

    }

    var refresh = window.setInterval("loadXMLDoc()",1000);


    //----ENDE AJAX REQUEST
    </script>

The xml file looks like this:

<Values>
<Value><![CDATA[<img src="status-gruen:="Eingabe_Web":.png" style="width:35px;"/>]]></Value>
<Value><![CDATA[:="Differenzdruck_Web":]]></Value>
<Value><![CDATA[:="Abreinig_Beginn_Web":]]></Value>
<Value>:="Abreinig_Ende_Web":</Value>
<Value>:="Abreinig_Alarm_Web":</Value>
<Value>:="Abreinig_Pause_Web":</Value>
<Value>:="Abreinig_Offline_Web":</Value>
<Value>:="Abreinig_Intervall_Web":</Value>  
<Value><![CDATA[<img src="status-gruen:="Ventilator_Web":.png" style="width:35px;"/>]]></Value> 
<Value><![CDATA[<img src="status-gruen:="Abreinigung_Web":.png" style="width:35px;">]]></Value> 
<Value><![CDATA[<img src="status-rot:="Alarm_Web":.png" style="width:35px;">]]>    </Value>
</Values>

Upvotes: 0

Views: 152

Answers (3)

semirturgay
semirturgay

Reputation: 4201

the browser cache may cause this problem. add a random query (that may be time stamp) string to your url to prevent cache

xmlhttp.open("GET","einlesen.xml?q="+(new Date()).getTime()+"",true);

Upvotes: 1

edisonthk
edisonthk

Reputation: 1423

Maybe cache causing this problems. Give a tried on secret mode on browser. Make sure you are using PC browser but not mobile.

For chrome shortcut, CTRL + SHIFT + N
For firefox shortcut, CTRL + SHIFT + P 

Upvotes: 0

Goran.it
Goran.it

Reputation: 6299

You should use a standard trick to prevent fetching the cached version of the static files.

Example:

var tstamp = new Date().getTime();
xmlhttp.open("GET","einlesen.xml?v="+tstamp,true);

Upvotes: 1

Related Questions