Making AJAX calls from a .NET web page - Part 1
AJAX, (which stands for “Asynchronous JavaScript and XML”) is a really cool technology. Although not a perfect solution, AJAX has solved some difficult problems related to creating a “rich” user experience in a web browser.
With a traditional web page, the browser makes an http request to the server. The web server delivers the HTML content to the browser and the connection between the browser and the server is then broken. If the user performs some action on the page (e.g. fills out a form), the browser has to do another full round trip to the server, resulting in a page refresh, in order to get any information back to do something as simple as data validation. This is a somewhat less than satisfying experience for the user, especially if the connection is slow.
Enter AJAX.
In this posting, I intend to cover the following subjects relating to AJAX:
-What AJAX is
-Reasons you would use AJAX
-Sample AJAX code that you can use on a .NET (.aspx) web page
A brief perspective
When I was first introduced to dynamic web page creation some time ago, I was very excited about the possibilities. No more static .html pages and no more stale content. Through the magic of the .php or .NET server pages, I could now introduce logic, database query results, 3rd party content and many other things on my sites. This immediately led me to try integrating web service content from such vendors as Amazon in an effort to monetize my traffic.
While the integration was successful, the results of doing so were less satisfying — especially when querying more than one remote web service from a single web page at a time. If there was any latency in one of the query connections, the whole page would block until all of the data was returned. From a user’s perspective, this often meant that they could be left staring at a blank web browser (especially noticable under IE) for as much as 20 seconds — just waiting for the multiple query results to come back.
How AJAX can help
What is AJAX?
As mentioned above, AJAX stands for “Asynchronous JavaScript and XML.” This means that it is JavaScript, executed in the browser, called in a manner that returns asynchronous results, frequently formatted as XML. Most modern browsers (IE, Firefox, Safari, Opera) support JavaScript, so it should be a seamless experience for your site users.
The bad thing about using traditional JavaScript is that all your business logic is delivered to the client browser. This makes it hard to manipulate any kind of passwords or trade secrets such as formulas with it. The cool thing about what AJAX adds, is that it lets you keep all your business logic, password validation, secret calculations, etc. where they belong — on the server.
Reasons you would use AJAX
Here are a few reasons for using AJAX on your web pages:
-You want to integrate more than one 3rd party data source from typical Web service suppliers and want to avoid the multiple connection latency problem.
-You want to do some kind of data validation of user input (e.g. filling out a registration form) without either delivering all the logic to the client in JavaScript or making a round trip to the server with the resulting full page refresh.
-You want your web application to have the more natural feel and responsiveness of a traditional GUI desktop application.