How to Call Web Services in Asp.net Ajax

Leave a Comment

Calling WebService in ASP.NET AJAX need to create a ServiceReference using ScriptManager Services and using ScriptReference tag.
<asp:ScriptManager runat="server" ID="SM1">
<Services>
<asp:ServiceReference Path="MathService.asmx" />
</Services>
</asp:ScriptManager>

Here it is assumed that you have created a MathService.asmx web service. Note that when you create a web service in ASP.NET, it by default writes following attribute.
/// <summary>
/// Summary description for MathService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService] 

Notice the last line is commented. In order to call this web service from ASP.NET AJAX, we need to uncomment the last line and the modified attribute of your web service should look something like below.
/// <summary>
/// Summary description for MathService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]

Now you are ready to call the web method

Download Source Code

0 comments:

Post a Comment