Calling Webservices from Client


This topic explains how to use the AJAX functionality of ASP.NET to call a Web service from ECMAScript (JavaScript). To enable your application to call ASP.NET Web services by using client script, the server's Web service communication layer automatically generates JavaScript proxy classes. A proxy class is generated for each Web service that is referenced by an ServiceReference element in the ScriptManager control in the page.

The Web services can be in the form of .ASP.NET Web services (.asmx services), or Windows Communication Foundation (WCF) services (.svc services). If you have already created ASP.NET Web services (.asmx), you can modify them to enable script in your AJAX-enabled Web pages to call them. For more information, see Exposing Web Services to Client Script. If you have already created WCF services, you can add end-points to enable script in AJAX-enabled Web pages to access the services. For more information, see Exposing WCF Services to Client Script.

To call a method of the Web service, you call the corresponding method of the generated JavaScript proxy class. The proxy class in turn communicates with the Web service.

Calling Web Service Methods

Calling a Web service method from script is asynchronous. To get a return value or to determine when the request has returned, you must provide a succeeded callback function. The callback function is invoked when the request has finished successfully, and it contains the return value (if any) from the Web method call. You can also provide a failed callback function to handle errors. Additionally, you can pass user context information to use in the callback functions.

The following example shows how to make the following types of Web service calls:

·         Calling a Web service that has no return value.

·         Calling a Web service that returns a value.

·         Calling a Web service method that takes parameters.

·         Calling a Web service method by using the HTTP GET verb.

·         Calling a Web service method that returns an XmlDocument object.


Comments