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.