February 20, 2019
Hot Topics:

Consuming Web Service Using ASP.NET AJAX

  • February 5, 2007
  • By Bipin Joshi
  • Send Email »
  • More Articles »

The pageLoad() function gets called automatically when the web page is loaded in the client browser. Finally, the error handler and timeout handler functions look like this:

function ErrorHandler(result)
   var msg=result.get_exceptionType() + "\r\n";
   msg += result.get_message() + "\r\n";
   msg += result.get_stackTrace();
function TimeOutHandler(result)
   alert("Timeout :" + result);

The TimeOutHandler() function is called whenever a web method times out. It simply shows an alert to the user. The ErrorHandler() function is called whenever there is any error. The result parameter received by this method provides three methods: get_exceptionType(), get_message(), and get_stackTrace(). These three methods return the type of exception, detailed error message, and stack trace, respectively. The ErrorHandler() function simply displays an alert to the end user.

Testing the Web Page

Now that you have developed the web service and the client application, it's time to test them. Run the web form and try inserting, updating, or deleting employees. Figure 5 shows the web form after updating an employee.

Figure 5: Web Form After Updating an Employee

To test the error handler function, deliberately initialize the connection string to an empty string and run the web page again. This time, you should see an alert as shown in Figure 6.

Click here for a larger image.

Figure 6: ConnectionString Property Alert

Calling External Web Services

In this example, the EmployeeService was part of your web site. Sometimes, you may need to call web services that are not hosted by your domain at all. ASP.NET AJAX internally relies on the XML HTTP object, which for security reasons cannot communicate outside the originating web site. That means the above technique won't work for external web services. Unfortunately, ASP.NET AJAX does not have a direct solution to this issue (at least in the RC version). However, Microsoft has released a "bridge" technology that is still in CTP stages. You can use the bridge to call a wrapper class residing in your web site, which in turn calls the actual web service. In the current RC version, you can create a wrapper web service in your web site, which in turn calls the original web service. From the client application, you then can call your wrapper web service and achieve the communication. The following is an outline of the necessary steps:

  1. Add a web reference to the external web service in your web site.
  2. Create a new web service in your web site.
  3. In the newly created web service, provide wrapper web methods that call the external web methods via web reference.
  4. Call this newly added web service from the client application as described in this article.

Infrastructure for Calling ASP.NET Web Services

ASP.NET AJAX provides a complete infrastructure for calling ASP.NET web services from client-side JavaScript. You can easily integrate server data with the user-responsive web pages using AJAX. All you need to do is mark your web service with the [ScriptService] attribute. The ASP.NET AJAX framework generates a JavaScript proxy for your web service. The proxy then is used to call the web methods.

Download the Code

Download the code that accompanies the examples in this article.

About the Author

Bipin Joshi is the founder and owner of BinaryIntellect Consulting, where he conducts professional training programs on .NET technologies. He is the author of Developer's Guide to ASP.NET 2.0 and co-author of three WROX press books on .NET 1.x. He also is a Microsoft MVP, member of ASPInsiders, MCAD, and MCT.

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date