Chapter 4. XML and JSON for Ajax
Do you really need XML for an Ajax application? The previous chapter showed that you don’t always need XML. In particular, if you only have one data point, XML is overkill. But the fact is, most web applications deal with multiple data points: usernames, passwords, addresses, cities, states, zip codes, etc. How will you decipher those fields when they’re sent back from the server?
In some cases, passing a string of delimited values may seem like the simplest approach, but using XML has advantages. For one thing, XML is self-documenting. During debugging, you can look at the XML string and see exactly what goes where; that is a luxury you won’t have with a string of comma-separated values.
Another reason for using XML is that an XML parser is built into most browsers. The parsing work has already been done for you; all you have to do is leverage the built-in parser. Sure, you could pass the data in other formats—Java properties files, comma or tab-separated values, YAML files, or a cute custom format that you’ve designed yourself—but then you would have to write your own parser in JavaScript.
Tip
There is another good way to send data to and from the server: JavaScript Object Notation (JSON). We will discuss JSON toward the end of this chapter.
The Character Decoder
The example in this chapter is similar to the one in the previous chapter, but instead of the server returning one data point, it’s going to return five. Retuning a small collection of data ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access