AJAX and PHP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 2<br />
}<br />
}<br />
else<br />
{<br />
// display status message<br />
alert("There was a problem retrieving the data:\n" +<br />
xmlHttp.statusText);<br />
}<br />
Apart from the error-h<strong>and</strong>ling bits, it's good to notice the xmlHttp.responseText method that<br />
reads the response from the server. This method has a bigger brother called<br />
xmlHttp.responseXml, which can be used when the response from the server is in XML format.<br />
Unless the responseXml method of the XMLHttpRequest object is used, there's really no<br />
XML appearing anywhere, except for the name of that object (the exercise you have just<br />
completed is a perfect example of this). A better name for the object would have been<br />
"HttpRequest". The XML prefix was probably added by Microsoft because it sounded<br />
good at that moment, when XML was a big buzzword as <strong>AJAX</strong> is nowadays. Don't be<br />
surprised if you will see objects called AjaxRequest (or similar) in the days to come.<br />
Working with XML Structures<br />
XML documents are similar to HTML documents in that they are text-based, <strong>and</strong> contain<br />
hierarchies of elements. In the last few years, XML has become very popular for packaging <strong>and</strong><br />
delivering all kinds of data.<br />
Incidentally, XML puts the X in <strong>AJAX</strong>, <strong>and</strong> the prefix in XMLHttpRequest. However, once again,<br />
note that using XML is optional. In the previous exercise, you created a simple application that<br />
made an asynchronous call to the server, just to receive a text document; no XML was involved.<br />
XML is a vast subject, with many complementary technologies. You will hear people<br />
talking about DTDs, schemas <strong>and</strong> namespaces, XSLT <strong>and</strong> XPath, XLink <strong>and</strong> XPointer,<br />
<strong>and</strong> more. In this book we will mostly use XML for transmitting simple structures of<br />
data. For a quick-start introduction to XML we recommend http://www.xmlnews.org/<br />
docs/xml-basics.html. If you don't mind the ads, http://www.w3schools.com/<br />
xml/default.asp is a good resource as well. Appendix C available at<br />
http://ajaxphp.packtpub.com contains an introduction to XSLT <strong>and</strong> Xpath.<br />
You can use the DOM to manipulate XML files just as you did for manipulating HTML files. The<br />
following exercise is similar to the previous exercise in that you read a static file from the server.<br />
The novelty is that the file is XML, <strong>and</strong> we read it using the DOM.<br />
Time for Action—Making Asynchronous Calls with XMLHttpRequest<br />
<strong>and</strong> XML<br />
1. In the foundations folder create a subfolder called xml.<br />
2. In the xml folder, create a file called books.xml, which will contain the XML structure<br />
that we will read using JavaScript's DOM. Add the following content to the file:<br />
<br />
55<br />
www.it-ebooks.info