Server-based AJAX
The server-based AJAX libraries all have a common approach:
- Enable you to take a method on the server and declare it as AJAX callable.
- Create a representation of that server API in JavaScript.
Virtually every server language, from the most mainstream to the more niche, has a server-based API to enable AJAX capabilities. These server-based libraries all have built-in capabilities for taking server objects and returning JavaScript representations of the objects on the client. This article covers a few options for ASP.NET, PHP, and Java.
ASP.NET
The server-based APIs that enable AJAX capabilities in ASP.NET are ASP.NET AJAX Extensions (formerly code-named "Atlas") from Microsoft (free but not open source) and AjaxPro (free and open source) from Michael Schwarz. ASP.NET AJAX Extensions has 100-percent server-coded options that create AJAX functionality on the client. It is a better option for those wanting to avoid client scripting. ASP.NET AJAX Extensions also allows you to create server APIs callable via client-based JavaScript.
AJAXPro doesn't offer the rich "automagic" components offered by ASP.NET AJAX Extensions, but it does offer the simplest experience when it comes to making a server API client-side callable.
AJAXPro offers an extremely simple but effective model:
- Add an attribute to a method declaring that the method should be AJAX callable.
- Call this method via JavaScript the same way you would call it on the server.
AJAXPro does all of the magic of turning return value objects like DataSets into JavaScript representations for you.
PHP
The most widely used PHP AJAX library is xAjax, which allows you to define functions as you normally would and then make them AJAX callable via a single line of code. In terms of raw market share (again according to the Ajaxian.com poll), xAjax rules the PHP universe and it's easy to see why. The syntax is simple, clean, and gets the job done with minimal hassle.
Java
Within the Java universe, you will find Direct Web Remoting (DWR) and the Google Web Toolkit (GWT). It's easier to get up and running with DWR, which involves less code to actually start making AJAX calls from the client than GWT. However, GWT offers much more complete debugging options and some big bonuses like nearly automatic support for the browser's back button.
The Choice Is Yours
If you are the type of person that wants to know every line of code your application executes, directly calling XMLHttpRequest is the only way to go. If you've got a Web application that needs to call multiple back-end services in different languages, or if you want to keep your JavaScript code developers completely separated from your server developers, then one of the JavaScript libraries is likely your best choice (see Table 2. AJAX Solution Recommendations for Users).
| AJAX Solution |
Best Suited for... |
| AJAXPro |
ASP.NET developers who are trying to create a fully client-side experience and just want to make a few server-side API calls to get/set data |
| ASP.NET AJAX Extensions ("Atlas") |
ASP.NET developers who want AJAX functionality via server components without writing JavaScript code |
| Direct Web Remoting (DWR) |
Java developers who want to get up and running with an AJAX solution quickly |
| Dojo |
Client-side developers who want a strong abstraction between them and the browser's DOMClient-side developers who want the most impressive end effects from the least code |
| Google Web Toolkit (GWT) |
Java developers who are willing to invest more time to get a more robust, easier-to-debug solution |
| Prototype |
Client-side developers who want minimal abstraction between them and the DOMClient-side developers who want a library to speed up common DOM manipulation tasks |
| xAJAX |
PHP developers of all stripes |
| Table 2. AJAX Solution Recommendations for Users |
Server language AJAX libraries are consistently the quickest routes to AJAX nirvana, as they can format complex server data types to JavaScript equivalents. Alternatively, you can always mix and match your approaches. If you like the Dojo JavaScript library's rich client functionality but want to use DWR to handle your AJAX, you can have the best of both worlds.