Computer Magic
Software Design Just For You
 
 



JSON – Another AJAX?

Here is an interesting article on JSON (http://www.informit.com/guides/content.asp?g=xml&seqNum=268). It has some great examples of what JSON really is and how it works.

Before we go any further though, we should spend some time discussing what exactly AJAX and JSON are.

AJAX is simply a new way of thinking in the JavaScript programming realm. In the past, we refreshed every web page any time anything changed. To get a more application like feel, an AJAX library allows you to easily create pages that appear to work without the refresh. There is still a refresh, you just don’t have to refresh the whole page. All that stuff you learned about GET and POST when learning web programming still applies.

All the browser companies added a new object that Javascript could instantiate. This object had the ability to make requests in the background. It is this core functionality that AJAX relies on. An AJAX library doesn’t do anything that you can’t do yourself, it just makes it MUCH easier and saves you from having to write and debug the plumbing code. Other than usign the new object, AJAX isn’t really even new technology (it is just a JavaScript file with some pre-built functions). The real shift is in the way you think. Now, instead of writing your forms so that they refresh the page, you write them so that only portions of the data are refreshed.

That is AJAX in a nutshell. I personally like to use the prototype.js library myself, though there are others out there and most of the big name companies are integrating AJAX support into their products.

The acronym stands for Asyncronous JavaScript and XML. Often the requests that an AJAX script will make will be returned as XML which can be easily parsed. Again, not a new technology, just put together in a new and interesting way to leverage existing technologies and make things work in a new way.

Now, on to JSON. JSON is not like AJAX. JSON (JavaScript Object Notation) is a way to describe objects and their properties in a string format. This allows you to package up an object and its data and send it from one location to another. JSON itself does not provide a mechanism that will allow you to transport the information, but it does provide a mechanism that will allow you to deal with the information once you get it. The article above shows the first few examples without the page retrieving information from another site. Notice that you can use JSON without a JSON library if you use the eval() function in JavaScript. Use of the eval() function in JavaScript can be very dangerous and therefore a JSON library is useful (it still uses eval internally I am sure, but it checks the code to make sure things are safe).

JSON is positioned to compete with protocols like SOAP which was designed to allow applicaitons to communicate easily. In the example article above, the author takes advantage of the src property of script tags to fool the browser into making a request (similar to AJAX but using a different method). I imagine you could use this same technique with AJAX to make requests to the remote server. The down side to this is that all requests are made via a GET request where AJAX can support GET or POST requests.

JSON is not positioned to take the place of AJAX, they are really different animals, but I can see the potential of utilizing the two together. Rather than have to resort in trickery to get your JSON information from a remote site, have your AJAX request return JSON information and send it to the JSON parser. This could potentially save you some coding when trying to parse the XML information returned via teh AJAX request.

Note, that the URL that you request can return whatever you make it return. Personally when using AJAX, I mostly just return rendered HTML so that I don’t have to write more javascript to do the same thing (not always the answer, but in my case it works great!). I can see JSON as a great compromise as the ability to have an object ready to go would allow me to write just a few lines of code and have the same end effect of working with all that XML. Don’t get me wrong, XML is great, but why make yourself work harder than you have to? If a quicker, easier method works, do it and save yourself some coding and debug time.

Correction: During further review of the prototype AJAX library, I noticed that it posseses JSON capabilities already. Something to keep in mind anyway.

All in all, some very good examples, go read the article!

Ray Pulsipher

Owner

Computer Magic And Software Design

Comments are closed.


Home | My Blog | Products | Edumed | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
This page has been viewed 826193 times.

Copyright © 2005 Computer Magic And Software Design
(360) 417-6844
computermagic@hotmail.com
computer magic