How to: Calling Web Services methods from Client Script in ASP.NET AJAX
Software Development May 12th, 2007
My previos post explained the way to expose a web service to client script in Ajax. This post will introduce the "How to" call the web service's method from the client script.
Our web service is:
123456789101112131415161718 |
namespace MySamples.BL{ [WebService(Namespace = " http://tempuri.org/")] [ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] public string PrintString(String input) { return input; } [WebMethod] public string SayHello() { return "Hello"; } }}
|
Calling a Web service method from script is asynchronous. Succeeded callback function must provided to get a return value or to determine when the request has returned. This function is invoked when the request has finished successfully with return value (as a parameter) from the Web method call. Failed callback function can also provided to handle errors.
Based on the previous post, this is what you should do in the client script.
The script is:
123456789101112131415161718192021222324 |
// This function calls the Web service method // without parameters and // passes the event callback function. function SayHello(){ MySamples.BL.MyWebService.SayHello(SucceededCallback);}
// This function calls the Web service method // This is the callback function invoked if the Web service succeeded. |
Thats all!
AJAX enables you to call ASP.NET Web services by using client script. There are several steps to enable the web service exposing.
1. Qualify the Web service class with the ScriptServiceAttribute attribute:
123456789 |
[ScriptService]public class MyWebService : System.Web.Services.WebService{ [WebMethod] public string PrintString(String input) { return input; }}
|
2. Configure the Web application to support calling Web services from script. Register the ScriptHandlerFactory HTTP handler in the Web.config file for the application:
<system.web>
<httpHandlers>
<remove verb=”*” path=”*.asmx”/>
<add verb=”*” path=”*.asmx” type=”System.Web.Script.Services.ScriptHandlerFactory” validate=”false”/>
</httpHandlers>
<system.web>
Note: The handler delegates the call to the default handler for Web service calls that are not issued from AJAX script.
3. Enable the Web service to be called from script in an ASP.NET Web page:
<asp:ScriptManager runat=”server” ID=”scriptManager”>
<Services>
<asp:ServiceReference path=”~/BL/MyWebService.asmx” />
</Services>
</asp:ScriptManager>
Note: The ServiceReference object can reference a Web service only in the same domain.
JavaScript proxy class for the .asmx Web Service created by the page that contains the ScriptManager control. It occures when this page is rendered. The proxy class has methods that correspond to each Web method in the MyWebService.asmx service.
The next post will be about the way to call the Web Service.
Microsoft announced Silverlight
Software Development May 4th, 2007
At Mix07, Microsoft announced Silverlight™.
Microsoft® Silverlight™ is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web.
You can read more here (SilverLight site), its amazing!
Great post about Silverlight UI Controls, by Ashish Shetty, you can find here.
Tags: .NET, AJAX, ASP.NET, Silverlight
About

