Tuesday, June 30, 2009

Consume webservice From JavaScript

1. Add an asp:ScriptManager in form of the aspx page.

2. Add the service that you want to access in the script manager tag.

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/ZipCodeService.asmx" />
</Services>
</asp:ScriptManager>

here ZipCodeService.asmx is the path of the web service. In this case the web service resides in the same project. If this does not reside in the same project then add web reference.

3. Ensure that the web service added can be invoked from script. In case of .net, this is ensured by adding the following attribute to the class of the web service.

In ZipCodeService, this is ensured by -

[System.Web.Script.Services.ScriptService]
public class ZipCodeService : System.Web.Services.WebService
{
. . .
}

4. Now this web service is eligible to call from JavaScript. So call the webmethod of web service with full namespace name.

For detail please visit here