Home > JQuery > How to call a web service using jquery?

How to call a web service using jquery?

JQury is a really powerful library which can be used to do many task. Its moto is do more write less to do more.
In this post I will explain you how to call a Webservice using JQuery.

You can call a webservice function from jQuery too, but it’s a lot more harder and lot less fun. Here’s how you can do that.

1. Decorate your web-service / pagemethod with the [WebMethod] attribute so that they serialize to JSON (jQuery does not support WSDL).
2. Make only POST requests.
3. Send an empty JSON variable to the web service if you don’t have any arguments.

The $.ajax function of jQuery can be used to make the request at the client end. Here’s a sample

$.ajax({ url: “/webservice.asmx/DoSomething”,
type: “post”,
data: “{}”,
contentType: “applicatin/json; charset=utf-8”,
dataType: “json”,
success: function(result) {
alert(msg.d);
},
error: function(err) {
alert(“error!”);
}
});

Notice that in the success callback function we the reference is to msg.d and not to the msg itself. This is because ASP.Net wraps the JSON serialized data in a variable called ‘d’. It’s a convention to improve data security.

Advertisements
Categories: JQuery Tags:
  1. March 1, 2013 at 7:18 am

    Very good article. I absolutely love this website.

    Continue the good work!

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: