Handling Data for Ajax with JSON - Changing the JavaScript for JSON
(Page 4 of 4 )
Now let's look at the client JavaScript code. I've replaced two functions with one: I removed msPopulate() and nonMSPopulate() , and now all browsers use the populateJSON() function shown in Example 4-12.
Example 4-12. The populateJSON( ) function
function populateJSON() {
var jsonData = req.responseText;
var myJSONObject = eval('(' + jsonData + ')');
var decimal = document.getElementById('decimal');
decimal.value=myJSONObject.conversion.decimal;
var hexadecimal = document.getElementById('hexadecimal');
hexadecimal.value= myJSONObject.conversion.hexadecimal;
var octal = document.getElementById('octal');
octal.value=myJSONObject.conversion.octal;
var binary = document.getElementById('bin');
binary.value=myJSONObject.conversion.binary;
var hyper = document.getElementById('hyper');
hyper.value=myJSONObject.conversion.hyper;
}
Summary
Remember that Ajax isn't a technology: it's a group of ideas that, used together, have proven very powerful. When you combine form manipulation with JavaScript, asynchronous callbacks withXMLHTTPRequest, and built-in XML with JSON parsers, you have something revolutionary--even though the individual pieces have been around for a while.
Combine these client-side technologies with Javas established server-side technologies, such as servlets, Struts, and JSF, and you've got a really powerful basis for building a new generation of interactive web applications.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Check it out today at your favorite bookstore. Buy this book now.
|
|