![](https://csdnimg.cn/release/download_crawler_static/4052700/bg5.jpg)
20
|
Chapter 4: XML and JSON for Ajax
XMLHttpRequest( ), and the server responds with a String containing the five conver-
sions in XML format (decimal, octal, hexadecimal, binary, and HTML). The
callback( ) function in the client then calls a function to parse the XML and popu-
late the fields in the browser.
Now it starts to get fun. One keystroke fills in all the data fields, and although it
doesn’t look like much is going on from the user’s perspective, from the program-
mer’s perspective we know that the application is communicating with the server
without a clunky submit or reload or any waiting for the page to refresh.
Setting Up a Simple XML Document
Before we delve into the code, we need to make some decisions. We’re going to
return data using XML, but how should that XML be structured? What should our
XML response look like? We don’t want anything complex, so we’ll aim to create an
XML document that looks like this:
<converted-values>
<decimal>97</decimal>
<hexadecimal>0x61</hexadecimal>
<octal>0141</octal>
<hyper>&0x61;</hyper>
<binary>1100001B</binary>
</converted-values>
With this format, the browser can use its document object model (DOM) parser to
index and retrieve the data.
Figure 4-1. The complete Ajax Character Decoder example