Consuming Web Services using AJAX and ASP.NET 3.5



Share3

In order to understand about consuming web services using AJAX, we must first bear in mind what AJAX actually is. The acronym AJAX actually stands for "Asynchronous Javascript and XML". Note the emphasis on "Javascript". Since AJAX is essentially a Javascript framework, it is subject to the same limitations as Javascript. The specific limitation we need to be concerned with when consuming web services via AJAX/Javascript is the restriction on cross site scripting.

What Is Cross Site Scripting, and Why Do We Care?

Cross Site Scripting simply refers to the calling of a script or application located in another domain directly from your client script. All modern browsers have restrictions in place that prevent cross site scripting, since it represents a security risk. What this means to us when consuming web services using AJAX is that calling a web service that we ourselves are hosting is going to be done in a completely different way than calling a remote web service. Therefore, I've broken the rest of this article into two sections: "Consuming A Local Web Service", and "Consuming A Remote Web Service".

Consuming A Local Web Service

Requirements:

Let's say your company is in the business of selling laptops. On your web site, you want to allow users to choose any laptop from a list of available laptops to learn more information about that particular laptop, such as description, RAM, disk size, processor speed, and price. You have an existing web service named ProductInfoWS that can retrieve this information. The web service has a method called GetProductInfo, which accepts a ProductID as its sole argument, and returns a string which contains detail information about that particular product. (We'll assume for this example that the string returned by the web service contains HTML that can be displayed as-is directly on our web page. In practice, formatting of the results should be handled by the presentation layer, not the web service.) We will use "Client-side network callbacks" to interact with the web service.

Solution:

From a new or existing project within Visual Studio 2008, add a new AJAX Web Form to the project and name it AJAXWebForm.aspx. (An AJAX Web Form is just like a Web Form, but it adds a ScriptManager component and some skeleton client-side Javascript.) The markup will look something like this:



Add your web service to the Services collection of the ScriptManager component, specifying the path as follows:



Add an HTML control that will trigger the call to the web service. In our case it will be a select control based on the requirements as outlined earlier. (Note that this must be an HTML control. We do not want to use an ASP.NET control in this case since we want this call to come from the client.) At this point, your markup should look something like that shown in the code segment below. I've added "div" tags for formatting, and a particular div (divDisplay) as a place holder for displaying the response from our web service.



Next we want to handle the select control's onchange event…



…and create the appropriate Javascript function:



Note that the GetProductInfo method of the ProductInfoWS web service takes four arguments in this case. The first argument is the ProductID as discussed in our requirements. The next three serve as delegates, or function pointers. The first of these "extra" parameters must specify the name of the function to be called in the event of a successful return by the web service. The second parameter contains the name of the function to be called in the event of a timeout, and the third contains the name of the function to be called in the event that the web service returns on error. Note that all three of these functions accept an argument. The argument will contain the response returned by the web service. Thus, for this example, I've chosen to display the results of the web service call within the"divDisplay" "div" tag regardless of success, failure, or timeout.

 

The entire thing looks like the listing below. Note that the code-behind file was never touched, since there is no server-side code involved.


AJAXWebform.aspx


Now that we've successfully consumed a local web service using AJAX, let's see about consuming a remote web service.


Consuming A Remote Web Service

Requirements:

Let's say you want to give visitors to your web site the ability to submit a ticker symbol and receive back a stock quote. Let's also assume that for the sake of this example, you know of a remote web service which does exactly that. In fact, I know of a free one we can use for this example. The URL is http://www.webservicex.net/stockquote.asmx. Since this is a remote web service, we can't do a client-side network callback due to restrictions on cross-site scripting enforced by browsers. We will use server-side ASP.NET controls to interact with the web service.

Solution:

From a new or existing project within Visual Studio 2008, add a new Web Form to the project and name it SSAJAXWebForm.aspx. Then, drag a ScriptManager component from the AJAX Extensions section of the toolbar to anywhere within the "form" tags. Your markup should now look something like this:



Next, add an UpdatePanel component by dragging it onto the form from the AJAX Extensions toolbar section. Within the UpdatePanel, create a ContentTemplate element, into which we will place all of our controls. For this example, we will need one each of the following controls: asp:TextBox, asp:Label, and asp:Button. The asp:TextBox control will be where the user enters the ticker symbol, the asp:Button control will be what the user clicks in order to initiate the activity, and the asp:Label control will be where we place the data returned by the web service. So, switch to Design View and drag them onto your Web Form into the ContentTemplate element, laying them out and formatting them as you like. Your markup should now look something like this:



Now it's time to wire up our button control with a server-side event handler. Switch back to design mode and double click on our asp:Button control. This will cause two things to happen: (1) a server-side event handler will be created in your code behind file (SSAJAXWebForm.aspx.cs) called btnGo_Click (assuming you used the name btnGo for the button's name), and (2) the client side html code will have been modified so that the asp:Button control is now wired up to the new event handler:



Hence, your SSAJAXWebForm.aspx file should end up looking like this:


SSAJAXWebForm.aspx


Next, we will add a Web Reference to our project that points to the remote web service. To do this, simply right-click on your project in the Solution Explorer, and click Add Web Reference from the sub-menu. Once the Add Web Reference dialog is displayed, simply enter the URL to the WSDL file of the remote web service and click "Go". If the wizard finds a valid web service at that url, it will display all of the methods exposed by the web service. Here's what the wizard should look like once you're done:



Once you are satisfied that the web service is valid, click the Add Reference button to add this to your project. Once we have a web reference to the web service, we can instantiate it and refer to it just as we would any other object. This particular web service returns an XML string which needs to be parsed once it's been received. The code snippet below should give you an idea what your event handler should look like. Please note that the resulting HTML string is passed back to the Web Form for display simply by assigning it to the Text property of the asp:Label control we created earlier. Note also that you should replace [Namespace] in the StockQuote instantiation with the name of your namespace.


Taken from SSAJAXWebForm.aspx.cs


Hopefully this article makes this topic less confusing, rather than more. You can see implementations of both the client-side and server-side AJAX methods discussed in this article in the Component Demos section of our web site at http://www.murrayhilltech.com .

Dave Verschleiser
Murray Hill Technologies


Similar Articles