I’m a big fan of jQuery. I’m no expert, but with a copy of jQuery in Action on my desk and Google Bing at my fingertips I can do a lot of cool things with it. One of the ways I often see people using jQuery is to make Ajax requests. jQuery provides a great library which should take care of just about all your Ajax needs, if you are an UpdatePanel-free web developer. I’ve blogged about it to a small degree, but for more in-depth details you can read about it here. For a quick example, here’s how you might make an Ajax call using jQuery:
$.ajax({
url: "Services/Customers.svc",
type: "GET",
cache: false,
success: getCustomersCompleteHandler
});
What amazes me is when I see folks using jQuery when all they need is Ajax functionality. If you’re using ASP.NET, you shouldn’t be surprised to know that it’s already provided for you in the client-side library. When you add service references to your ScriptManager, the JavaScript proxies it generates aren’t magic code; they make explicit calls into the ASP.NET AJAX library to perform service calls. You can too!
Using the WebRequest class, making Ajax calls is just as easy with ASP.NET AJAX:
function getCustomers(startIndex, rowCount) {
var request = new Sys.Net.WebRequest();
var url = String.format("Services/Customers.svc/?i={0}&c={1}", startIndex, rowCount);
request.set_url(url);
request.set_httpVerb("GET");
request.add_completed(getCustomersCompleted);
request.invoke();
}
Upon completion of the request you can access returned data or handle timeouts and aborted requests through the WebRequestExecutor object that is passed to the event handler.
function getCustomersCompleted(executor, args) {
if (executor.get_responseAvailable()) {
var data = executor.get_responseData();
var jsonData = eval("(" + data + ")");
loadContent(jsonData);
}
else if (executor.get_timedOut()) {
// handle request timeout
}
else if (executor.get_aborted()) {
// handle aborted request
}
}
I built a very simple client-side “grid” that offers paging functionality and retrieves all its data from a web service. You can see that each request made from the ASP.NET AJAX library invokes the web service and returns only the minimum amount of data necessary to update the grid.


Download the [SOURCE CODE] to see it in action.
If you’re already using jQuery to add other rich functionality to your site then it makes sense to use it for your Ajax needs as well. But if all you need is Ajax, don’t forget that the ASP.NET AJAX library offers the same functionality without the need to manage yet another client-side library in your web app. It’s good to have options.
