Friday, August 04, 2006

JSON Communications Module

I'm posting this here mainly for my own reference, but hey, other people may benefit, too, so...

I've written a JSON communications module to make it easy to send JSON-based content back and forth between a server and a Javascript application on the client side. In other words, AJAX, but with JSON as the format instead of XML. (I guess you'd call it AJAJ or AJAJSON for "Asynchronous Javascript and JSON", but that sounds dumb. Maybe something like SONJA or JASON, an anagram using the letters from AJAJSON. Sure, it uses fewer letters, but then, so does JSON compared to XML. Heh.)

Here's how it works. Suppose you have a Javascript object you want to perform an action with that requires communication with a server. You set up an action with a meaningful (unique) name for that action, and then when you want to take that action, you "fire" the action with some postargs. This code handles setting up the connection with the server, sending the postargs, receiving the JSON code from the server, parsing the JSON into a Javascript object, and then calling the given method in the given object with the result.

Dependencies are Prototype and JSON in Javascript. (Truth be told, most of the heavy lifting is done by these libs. My code is just glue.)

Anyhoo, the code can be found here.

No comments: