TulligWeather.Net: Home
Using AJAX To Update Data On A Web Page PDF Print E-mail
Written by David Hollingworth   
Thursday, 19 July 2007

The Responder Script

The job of the responder script is to send the XML file back to the web browser when the browser makes a request for it. I used the Ruby language for my script and here it is:
 
#!/usr/bin/ruby

#
# Define any constants
#
XMLFile = 'The name and path of your XML file goes in here. E.g. /home/me/myfile.xml'

#
# Print the headers
#
 puts "Content-type: text/xml"
 puts "Cache-Control: no-cache, must-revalidate"
 puts "Expires: Mon, 26 Jul 1997 05:00:00 GMT"
 puts "\n\n"

#
# Open the file and loop through its contents putting them to the output
#
open(XMLFile).each { |f| print f } 
 
exit
 
This file has to go into the cgi-bin directory on your web server and must be made executable. Becuase not all web services provide Ruby here's a PHP version of the same thing:
 
<?php

$handle = fopen("The name and path of your XML file goes in here. E.g. /home/me/myfile.xml", "r");

if ($handle)
{
    //
    // Send the appropriate headers
    //
    header('Content-Type: text/xml');
    header("Cache-Control: no-cache, must-revalidate");
    //A date in the past
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    //
    // Read to the end of the file putting the contents on the
    // output
    while (!feof($handle)) {
        $buffer = fgets($handle);
        echo $buffer;
    }
    fclose($handle);
}

?>

NB. I've not tested the above PHP script; but it should work.

 

You can save the PHP script as a .php file (e.g. responder.php) anywhere you fancy on your web server. It doesn't have to be in the cgi-bin directory.

 

To test that the XML file and responder script are working then you can call them directly in your web browser. For example: http://your-host.domain/responder.php. If all is well you'll get a rendition of your XML file in your browser. Possible problems are:

 

  • If you get a page not found (404) error then you're addressing your responder script incorrectly.
  • If the browser reports an error with the XML file then the most likely cause is a typo in one of the XML tags. For example:
<curtemp>^vxv007^</currtemp>
 
The start and end tags don't match.
 
If the browser displays the XML OK then we're into the final stage, the Javascript.
Last Updated ( Thursday, 19 July 2007 )