How to call webservice method by ajax.

Its very easy to call webservice method from client side using ajax. Following steps are the minimal things needed to

call webservice method from page using ajax :

1. Create a website using Visual studio(Here I am using Visual studio 2010), Lets name it WSCallFeatureTest.

2. Now add a new webform lets name it CallWebservice.aspx.

3. Add a new webservice file(.asmx) name it WebService.asmx

4. Add a webservice method to the WebService.asmx file. Below is the sample method by which I will test.

The content of the webservice class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = “;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components

public string HelloWorld() {
return “Hello World”;


5. Now its time to call the webservice method HelloWorld from the webform. To call the webservice method from the webform
have to add the ScriptManager to the page and have to add the webservice referrence to the page. The following content serve

this things.
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePartialRendering=”true”>
<asp:ServiceReference Path=”~/WebService.asmx” />

and a button will be added to the page this button will call the webservice method. The total content of the web form is

given below :

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”CallWebservice.aspx.cs” Inherits=”CallWebservice” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”&gt;

<html xmlns=””&gt;
<head runat=”server”>
<script type=”text/javascript” language=”javascript”>

function GetWSValue() {


return false;

function WsCompleted(result) {

if (result != null) {

<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePartialRendering=”true”>
<asp:ServiceReference Path=”~/WebService.asmx” />
<asp:Button ID=”btnGetValue” runat=”server” Text=”GetWSValue” OnClientClick=”return GetWSValue();”  />

6. Clicking on the button will call webservice method HelloWold and will show the result on the alert.


One Response to How to call webservice method by ajax.

  1. P.Thirunavukkarasu says:

    i don’t know how to call the webservice from the client page…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: