AJAX simple data retrieval from server

Posted on 2011-10-17
Last Modified: 2013-11-05
I'm trying to write a "simple" web page to test data retrieval from my server into XML format for use in a web page. What happens is, when I click the submit button, the "wait" animation (attached) displays forever. I guess it's not getting what it expects from the server? If I run the php script separately, it seems to generate the expected XML. This is the server side php script:

header('Content-Type: text/xml');
echo "<?xml version=\"1.0\" ?><clock1><timenow>" .date('H:i:s')."</timenow></clock1>";

Open in new window

And here is the web page:

<title>Ajax Demonstration</title>
.displaybox {
border:2px solid #000000;
font:24px normal verdana, helvetica, arial, sans-serif;
<script language="JavaScript" type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
return req;

var http = getXMLHTTPRequest();

function getServerTime() {
  var myurl = 'telltimeXML.php';
  myRand = parseInt(Math.random()*999999999999999);
  var modurl = myurl+"?rand="+myRand;"GET", modurl, true);
  http.onreadystatechange = useHttpResponse;

function useHttpResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
       document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
  } else {
  document.getElementById('showtime').innerHTML = '<img src="anim.gif">';
<body style="background-color:#cccccc" onLoad="getServerTime()">
<h1>Ajax Demonstration</h1>
<h2>Getting the server time without page refresh</h2>
<input type="button" value="Get Server Time" onClick="getServerTime()">
<div id="showtime" class="displaybox"></div>

Open in new window

animation gif
Question by:silentreproach
    LVL 15

    Accepted Solution

    use firebug and put breakpoints in the javascript
    also see the console has no errors
    LVL 41

    Assisted Solution

    You could add alert() statements to your callback routine (i.e., useHttpResponse()) to verify that it is, in fact, being called.

    Author Comment

    Strangely, it's working on my WinXP computer with Firefox 7.0.1, but not on my Win7 computer with the same version of Firefox. It doesn't work with Chrome on either computer.

    Firebug seems promising, but it seems to behaves differently on each browser and OS. I've added some alerts also, so I'll see if one of these approaches leads me to a solution first.

    Author Comment

    So far I'm finding that when the script does not work, http.status is 0 instead of 200.
    LVL 41

    Expert Comment

    LVL 41

    Expert Comment

    You might consider using something like WireShark ( to see what data is actually flowing over the connection.

    Author Closing Comment

    The responders gave me the debugging tools I needed to find my solution. Thanks very much!!
    LVL 41

    Expert Comment

    Thanks for the assist, and the points.

    Good luck & have a great day.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now