Introducing ASP.NET AJAX and Web Services

Find out how ASP.NET AJAX Extensions lets you use Web services to support JSON messaging.

ASP.NET AJAX provides controls such as the UpdatePanel that allow you to AJAX-enable new or existing Web pages without writing a lot of code. The UpdatePanel works by sending key information — including ViewState data, control values and page header information — from the client back to the server. The server processes the posted information and returns updated HTML and ViewState data. The data is dynamically added into the page on the client-side using JavaScript.

While this process works well, messages exchanged between the AJAX client and server can become very large, depending upon the amount of HTML and ViewState information being passed between them. Web services provide a way to minimize the size of request and response messages sent between the client and server. Instead of sending control values, ViewState data and other information, Web services only send the data needed by an application. They leave it up to the client to format data. It is similar to the process for calling a database and retrieving records. When a database is queried it returns a result set that represents the raw data. The client must format the data and make it look presentable to the user.

In addition to returning raw data needed by an application, Web services also allow data processing to occur on the client-side, as opposed to on the server. This behavior helps to minimize server load and results in faster initial-page load times.

Web Services typically use Simple Object Access Protocol (SOAP) to exchange data with a client. SOAP is a form of XML that is standardized, cross-platform and flexible. (For more information about it, visit http://www.w3.org/TR/2007/REC-soap12-part0-20070427.) Most platforms can process XML, which makes SOAP a good choice for data exchange.

However, in the case of AJAX applications, not all modern browsers provide support for XML processing and parsing. This means that SOAP is difficult to work with on the client-side. Instead, modern browsers support a specialized way to "markup" object data called JavaScript Object Notation (JSON). JSON provides several benefits including cross-browser support and the ability to exchange data among distributed applications in a compact messaging format.

JSON uses several delimiter characters such as { } and [ ] to serialize name/value pairs and arrays. While XML requires that every starting tag has a closing tag, JSON only requires that the { and } characters properly match up. For example, if you need to pass the name of a country to a service so that you can retrieve customers that live in that country, you can pass the following JSON message:

{"Country":"Argentina"}

This simple JSON message contains the data item's name (in C# or VB.NET terms, think of it as the property name) as well as the value. Notice that the name and value are wrapped using the { and } characters. Multiple name/value pairs can be added and separated with a comma:

{"Country":"Argentina",
 "Continent":"South America"}

After passing the country value to a service that returns customer data, you receive the message in Listing 1. It contains an array of customers serialized into the JSON format.

Looking through this message data, you'll see that the [ and ] characters are used as a container for array items. In this example, 3 customer objects are returned from the service, each with Country, CompanyName, CustomerID and ContactName properties.

.NET Web services don't natively work with JSON messages, but Microsoft's ASP.NET AJAX Extensions provides a way to easily allow services to support JSON messages so you can use them with little effort. In the next few columns, you'll learn how to create and consume Web services using ASP.NET AJAX, and exchange JSON messages. There's a lot of useful information to cover so stay tuned!

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.