AJAX, Asynchronous JavaScript and XML is a fancy way of saying "Make the browser retrieve and parse some XML", the important line of (JavaScript) code i want to talk about here is:
request.open("GET", "some-file-on-the-server.xml", true);
In my case i was using it to process an XML file containing data for google maps, the XML document is in the following format:
<agents lat="" lng="" company_name="" individual_name="" additional_info="" phone="" mobile_phone="" fax="" email="" url="" />
As outlined in the summary what I needed to do was this:
request.open("GET", "http://www.some-site.tld/some-file-on-the-server.xml", true);
Currently only firefox (>3.5) supports this kind of thing with minor changes in XML and code, but since I had to maintain browser support for the big 5 I couldn't use this method.
What I decided to do was keep one XML document on one server then use PHP to handle the rest on all other servers, lets say our XML document is at http://www.max.com/document.xml, for each site i would like that to be available I used the following.
AJAX Request:
request.open("GET", "cross_site_xml_engine.php", true);
cross_site_xml_engine.php - code:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/xml");
echo "<!--Cross site XML Engine: Written by MaxMouse\nThe below contents exist at http://www.max.com/document.xml\n-->\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.max.com/document.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;
?>
So now, on each server i have the above code, the AJAX request is happy because I'm requesting a file that resides on the same domain, as you can see the PHP code uses cURL to get the data from http://www.max.com/document.xml, it changes the header to XML and simply outputs the result, as far as the AJAX (JavaScript) request is concerned cross_site_xml_engine.php is simply an XML file. Whenever i update http://www.max.com/document.xml all other servers echo the change instantly without me having to re-upload to each of them, and the browser won't cause inconsistency with caching issues because I have modified the cache-control header. |