Here is how we've implemented Ajax on the Google App Engine, but the idea can be generalized to other platforms.
We have a handler script for Ajax requests that responds -mostly- with JSON responses. The structure looks something like this (this is an excerpt from a standard GAE handler script):
def Get(self, user):
self.handleRequest()
def Post(self, user):
self.handleRequest()
def handleRequest(self):
'''
A dictionary that maps an operation name to a command.
aka: a dispatcher map.
'''
operationMap = {'getfriends': [GetFriendsCommand],
'requestfriend': [RequestFriendCommand, [self.request.get('id')]],
'confirmfriend': [ConfirmFriendCommand, [self.request.get('id')]],
'ignorefriendrequest': [IgnoreFriendRequestCommand, [self.request.get('id')]],
'deletefriend': [DeleteFriendCommand, [self.request.get('id')]]}
# Delegate the request to the matching command class here.
The commands are a simple implementation of the command pattern:
class Command():
""" A simple command pattern.
"""
_valid = False
def validate(self):
""" Validates input. Sanitize user input here.
"""
self._valid = True
def _do_execute(self):
""" Executes the command.
Override this in subclasses.
"""
pass
@property
def valid(self):
return self._valid
def execute(self):
""" Override _do_execute rather than this.
"""
try:
self.validate()
except:
raise
return self._do_execute()
# Make it easy to invoke commands:
# So command() is equivalent to command.execute()
__call__ = execute
On the client side, we create an Ajax delegate. Prototype.js makes this easy to write and understand. Here is an excerpt:
/**
* Ajax API
*
* You should create a new instance for every call.
*/
var AjaxAPI = Class.create({
/* Service URL */
url: HOME_PATH+"ajax/",
/* Function to call on results */
resultCallback: null,
/* Function to call on faults. Implementation not shown */
faultCallback: null,
/* Constructor/Initializer */
initialize: function(resultCallback, faultCallback){
this.resultCallback = resultCallback;
this.faultCallback = faultCallback;
},
requestFriend: function(friendId){
return new Ajax.Request(this.url + '?op=requestFriend',
{method: 'post',
parameters: {'id': friendId},
onComplete: this.resultCallback
});
},
getFriends: function(){
return new Ajax.Request(this.url + '?op=getfriends',
{method: 'get',
onComplete: this.resultCallback
});
}
});
to call the delegate, you do something like:
new AjaxApi(resultHandlerFunction, faultHandlerFunction).getFriends()
I hope this helps!