This code sample demonstrate how to pass with or without parameters to WCF service / web service method from client side code ajax jquery json js.
Unlike Javascript, Jquery provides rich access to service method in asp.net c#
Unlike Javascript, Jquery provides rich access to service method in asp.net c#
With Parameters Sample
Client side code with dynamic and static text parameter to web service method from ajax json js With Parameters:-
function CallService()
{
var msg = ‘test message’;
$.ajax(
{
Type: "POST",
contentType: "application/json; charset=utf-8",
url: "ServiceName.asmx/WebMethodName",
data: "{ 'message': '"+msg+"' , ‘name’: 'test name' }",
success: function (msg) {
alert(msg.d);
}
});
{
var msg = ‘test message’;
$.ajax(
{
Type: "POST",
contentType: "application/json; charset=utf-8",
url: "ServiceName.asmx/WebMethodName",
data: "{ 'message': '"+msg+"' , ‘name’: 'test name' }",
success: function (msg) {
alert(msg.d);
}
});
Server Side webservice / WCF method code :
[WebMethod()]
public static string WebMethodName(string message, string name)
{
return message + “--” + name;
}
public static string WebMethodName(string message, string name)
{
return message + “--” + name;
}
Without Parameters Sample
Client side code without parameter to web service method from ajax json js With Parameters:-
function CallService()
{
$.ajax(
{
Type: "POST",
contentType: "application/json; charset=utf-8",
url: "ServiceName.asmx/WebMethodName",
data: "{ }",
success: function (msg) {
alert(msg.d);
}
});
Server Side webservice / WCF method code :
[WebMethod()]
public static string WebMethodName()
{
return “success”;
}