Monday, 30 July 2012

Call or acess wcf webservice using ajax jquery code in asp.net c# javascript js

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#
 
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); 
         } 
     });

Server Side webservice / WCF method code :
 
[WebMethod()]
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”;
}

No comments:

Post a Comment