Link to home
Start Free TrialLog in
Avatar of n2alexan
n2alexan

asked on

AJAX response returns the same value only requesting current date time

Hi,

I have a very simple webapp that has this index.htm page:

<!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>
    <title>Untitled Page</title>

    <script type="text/javascript">
            
            function GetXmlHttp() {      
      var xmlhttp = false;
      if (window.XMLHttpRequest)
      {
            xmlhttp = new XMLHttpRequest();
  }
      else if (window.ActiveXObject)// code for IE
      {
            try
            {
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                  try
                  {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (E) {
                        xmlhttp=false;
                  }
            }
      }
      return xmlhttp;
}

            function ajaxFunction(url)
  {  
    var xmlHttp = GetXmlHttp();
 
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("ajaxin").value = document.getElementById("ajaxin").value + '|' + xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
   }
   

    </script>
   
</head>
<body>
    <input id="Button1" onclick="ajaxFunction('ajax/tracker.aspx');" type="button" value="button" />
    <input id="ajaxin" style="width: 100%; height: 544px" type="text" />
</body>
</html>

======
and I use aspx page to write back the date and time

<% Response.Write(DateTime.Now) %>

but every time I press the button I get the same datetime and it's value is not changed, even after 1 minute.
The values of seconds is not updated.

Could someone explain what is happenning here.

Thank you very much,
N.

ASKER CERTIFIED SOLUTION
Avatar of mark-b
mark-b
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of n2alexan
n2alexan

ASKER

Hi Mark,

Thank you for your response. Your solution does work!

I have found the explanation to the problem by searching for 'ajax caching' terms in Google. I came across David's blog http://www.enja.org/david/?p=25 who has stated Microsoft Internet Explorer Cache issues which he found  on this http://en.wikipedia.org/wiki/XMLHTTP page.

"
Microsoft Internet Explorer cache issues
Internet Explorer implements caching for GET requests. Authors who are not familiar with HTTP caching expect GET requests not to be cached, or for the cache to be avoided as with the refresh button. In some situations, failing to circumvent caching is a bug. One solution to this is to use the POST request method, which is never cached; however, it is intended for non-idempotent operations.

Setting the "Expires" header to reference a date in the past will avoid caching of the response.
" (XMLHttpRequest - Wikipedia, the free encyclopedia, Wikipedia 2007).


Glad to help.  And good luck!

Cheers,

-Mark