How to call the webservice using Ajax on client side?

I hope that you are knowing the basic concept of web service.
There are so many method to call the web service.
1.Using Ajax (script manager control) and JavaScript
2.using Jquery.
3.Using C# code.

Here i m going to show you using script manager and Javascript.
Step1. Create the simple webservice like this, and UnComment this line

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

/// Summary description for WebService
[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”;


Step2: Write the Javascript code in default page like this

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

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

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


WebService.HelloWorld(OnComplete, OnTimeOut, onerror);

return true;
function OnComplete(args) {
document.getElementById(‘Label1’).innerHTML = args;
function OnTimeOut(args) {
alert(“Service call timed out.”);

function OnError(args)

alert(“Error calling service method.”);


<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” >
<asp:ServiceReference Path=”WebService.asmx” />
<input id=”Button1″ type=”button”
value=”button” onclick=”return onClick();” />
<br />
<br />
<label id=”Label1″ style=”width: 318px”></label>



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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.