Calling an asp.net web service from jQuery

As I have post it in earlier post that jQuery is one of most popular JavaScript library in the world amongst web developers Lets take a example calling ASP.NET web service with jQuery . You will see at the end of the example that how easy it is to call a web service from the jQuery.

Let’s create a simple Hello World web service. Go to your project right click->Add –> New Item and select web service and add a web service like following. Adding  a webservice 

Now modify the code of web service like following.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script,
using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{
[WebMethod]
public string PrintMessage()
{
return "Hello World..This is from webservice";
}
}

Here please make sure that [System.Web.Script.Services.ScriptService] is un commented because this attribute is responsible for allowing web service to be called by Client side scripts.

Now to call this  web service from jquery we have to include jQuery Js like following. I am already having in my project so i don’t need to include in project just need to add script tag like following.

<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"> 
</script>

Now let’s add some JavaScript code to call web service methods like following.

 <script language="javascript" type="text/javascript">
function CallWebServiceFromJquery() {

$.ajax({
type: "POST",
url: "HelloWorld.asmx/PrintMessage",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});

}

function OnSuccess(data, status)
{
alert(data.d);
}

function OnError(request, status, error)
{
alert(request.statusText);
}
</script>

Here I have written three functions in JavaScript First one is CallWebserviceFromJquery which will call the web service. The another two functions are delegates of   web service if Success is there then onSuccess Method will be called and If Error is there then OnError method will be called.

Inside the CallWebserviceFromJquery I have passed some parameter which will call web service with Ajax capabilities of $ jQuery object. Here are the description for each parameter.

Type: This can be GET or POST for web service we have to take POST as by default web service does not work with GET to prevent Cross site requests.

Url: Here will be url of the web service. You have insert fully qualified web service name here.

Data:Here it will be Empty as we not calling web service with parameters if you are calling web service with parameter then you have to pass that here.

contentType: here you have to specify what type of content is going to return by web service.

datatype:Json it will be result type

sucess:Here I have called the OnSuccess when the call is complete successfully. If you check the OnSuccess method you will see that I have set the returned result from the web service to the label. In the OnSuccess method body you see ‘data.d’. The ‘d’ here is the short form of data.

Error: Same as I have done with OnSuccess. If any error occurred while retrieving the data then the OnError method is invoked.

Now let’s add a asp.net button for on client click we will call the javascript function like following.

 

 <asp:Button ID="btnCallWebService" runat="server" 
OnClientClick="CallWebServiceFromJquery()"
Text="Call Webservice"/>

 

Now Let’s run it and here is the output in browser.

Ouput

Hope this will help you!!!.. Happy programming..

Shout it
Published Sunday, August 29, 2010 2:50 PM by Jalpesh P. Vadgama

Comments

# Twitter Trackbacks for Calling an asp.net web service from jQuery - DotNetJaps [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Calling an asp.net web service from jQuery - DotNetJaps         [asp.net]        on Topsy.com

# How to Hack Nike+ for Automatic Foursquare Check-ins | Swiss brand watches

Pingback from  How to Hack Nike+ for Automatic Foursquare Check-ins | Swiss brand watches

# How to send an unpaid debt to collection? | ach-debit

Friday, September 17, 2010 4:58 PM by How to send an unpaid debt to collection? | ach-debit

Pingback from  How to send an unpaid debt to collection? | ach-debit

# Where can you find a reputable debt management company? | ach-debit

Pingback from  Where can you find a reputable debt management company? | ach-debit

# How long should i wait to ask for a recommendation letter/reference if I&#8217;m volunteering? | lawyer

Pingback from  How long should i wait to ask for a recommendation letter/reference if I&#8217;m volunteering? | lawyer

# Do I really need to register my trademark ? | lawyer

Friday, September 17, 2010 9:26 PM by Do I really need to register my trademark ? | lawyer

Pingback from  Do I really need to register my trademark ? | lawyer

# What are Intellectual Property Rights on starting a on-line business? | lawyer

Pingback from  What are Intellectual Property Rights on starting a on-line business? | lawyer

# Air National Guard&#8217;s service saluted | ach-debit

Saturday, September 18, 2010 4:24 PM by Air National Guard’s service saluted | ach-debit

Pingback from  Air National Guard&#8217;s service saluted | ach-debit

# Internet abuzz with Gmail telephone calls | ach-debit

Saturday, September 18, 2010 9:02 PM by Internet abuzz with Gmail telephone calls | ach-debit

Pingback from  Internet abuzz with Gmail telephone calls | ach-debit

# Does anyone know of a real good credit repair agency? | credit

Thursday, September 23, 2010 11:25 PM by Does anyone know of a real good credit repair agency? | credit

Pingback from  Does anyone know of a real good credit repair agency? | credit

# Why you need a &#8216;zombie apocalypse&#8217; phone | credit

Friday, September 24, 2010 5:09 AM by Why you need a ‘zombie apocalypse’ phone | credit

Pingback from  Why you need a &#8216;zombie apocalypse&#8217; phone | credit

# re: Calling an asp.net web service from jQuery

Wednesday, November 03, 2010 1:20 PM by Ken Kondo

Totally ILL!  Thanks for this...

# dark under eye circles

Thursday, March 10, 2011 10:18 PM by dark under eye circles

Pingback from  dark under eye circles

# Traffic Travis 4 Review

Monday, April 18, 2011 10:33 PM by Traffic Travis 4 Review

Pingback from  Traffic Travis 4 Review

# re: Calling an asp.net web service from jQuery

Friday, June 17, 2011 1:29 PM by syed.tayyab.ali

awesome post.

# re: Calling an asp.net web service from jQuery

Friday, March 16, 2012 5:00 PM by ray

I got error in popup "Unknow".

I do the same code you did in above.

# re: Calling an asp.net web service from jQuery

Tuesday, March 20, 2012 2:46 AM by Jalpesh P. Vadgama

@Ray- Can you please check for the URL of web service. Still if there is a problem send me code.

# re: Calling an asp.net web service from jQuery

Tuesday, May 01, 2012 3:19 AM by Bilal Salas

Thaaaaaaanx

This if very usefull subject,and I had a problem but I didn't find the solution any where but here.

Thanx a lot.

Leave a Comment

(required) 
(required) 
(optional)
(required)