troubleshooting Question

readyState does not reach 4

Avatar of JF0
JF0Flag for United States of America asked on
JavaScriptAJAX
1 Comment1 Solution646 ViewsLast Modified:
I have this example done in 2 ways. The first way works but the second does not and I would prefer to use the 2nd method (or at least figure out why it does not work).

Working:

<script type="text/javascript">
function showImage()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
alert("4!")
  {
  document.getElementById("placeholder").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","image.asp",true);
xmlhttp.send(null);
}
</script>


Not Working:

      <script type="text/javascript">

      function showImage()
      {
      
      var xmlhttp;

      xmlhttp=GetXmlHttpObject();
      if (xmlhttp==null)
        {
        alert ("Your browser does not support AJAX!");
        return;
        }
      xmlhttp.onreadystatechange=stateChanged;
      xmlhttp.open("GET","image.asp",true);
      xmlhttp.send(null);
      }

      function stateChanged()
      {
      if (xmlhttp.readyState==4)
        {
        document.getElementById("placeholder").innerHTML=xmlhttp.responseText;
        }
      }

      function GetXmlHttpObject()
      {
      if (window.XMLHttpRequest)
        {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
        }
      if (window.ActiveXObject)
        {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
        }
      return null;
      }

      </script>

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros