WCF services can be consumed in javascript using the JQuery api.
JQuery can invoke services exposed with all Http bindings - webHttpBinding, basicHttpBinsing, wsHttpBinding.
JQuery supports only HTTP bindings of WCF, bacause the browser can send/receive only Http (or xmlHttp) messages and JQuery is limited by browser technology. JQuery method $.ajax() supports only Http methods of GET, POST, PUT, DELETE.
GET and POST methods are the most frequently used methods.
GET request is sent via the URL string (appended to the URI with a question-mark as separator), which is visible whereas POST request is encapsulated in the body of the Http request and cannot be seen.
Since GET goes via URL, the maximum size of the GET request is limited to 255 bytes. There is no such limitation on POST request. So I prefer using POST method in all my code below. GET may also be used; both the ways are given below.
Since the web message format that we use is JSON (JSON is smaller than XML), for each operation contract in the service interface, we need to write
[OperationContract(Name = "GetMessages")]
[WebGet(UriTemplate = "/MessageId={messageId}",
ResponseFormat = WebMessageFormat.JSON,
RequestFormat = WebMessageFormat.JSON)]
Message GetMessages(int messageId);
OR specify different format of RESTful URI below
[OperationContract(Name = "GetMessages")]
[WebGet(UriTemplate = "/MessageId/{messageId}",
ResponseFormat = WebMessageFormat.JSON,
RequestFormat = WebMessageFormat.JSON)]
Message GetMessages(int messageId);
The WebGet attribute exposes the method to GET operations so that the method is directly accessible via a web browser by typing the URI on address bar of the browser and the parameters to the service method can be sent via querystring.
The WebInvoke attribute exposes the method to other Http operations like PUT, POST, DELETE
Both the above attributes have UriTemplate property to specify the format of the RESTful URI.
<serviceMetadata httpGetEnabled="true"> is added to the service config file to enable the user to view metadata through web browser and generate WSDL file.
Set includeExceptionDetailInFaults="true" allows the WCF service to throw original error (useful for debugging).
Add <webHttp/> to endpoint behavior and webHttpBinding to enable web programming model for WCF and allow the service to be accessible through web protocols (Http).
webHttpBinding
This is the ideal way of calling WCF service from JQuery. The name value pair of input parameter and value can be passed as part of the URL or as data parameter of $.ajax() method. However all the data passed can be seen as part of the URL, so it may not be always best for security considerations.
Below is the sample of the web.config for the WCF service.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
The command for invoking the service in JQuery is below.
$.ajax({
type: "GET",
url: "../Services/Dashboard.svc/GetMessages?MessageId=4", // the name value pair
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: false,
success: function(result) {
var messsage = result.d;
},
error: function() { alert("Error"); }
});
This is the same as below statement. The name value pair input is sent as data parameter when POST method is used.
$.ajax({
type: "POST",
url: "../Services/Dashboard.svc/GetMessages",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{ MessageId:" + 4 + "}", // the name value pair input
processData: false,
success: function(result) {
var messsage = result.d;
},
error: function() { alert("Error"); }
});
basicHttpBinding
This is the same as
JQuery can invoke services exposed with all Http bindings - webHttpBinding, basicHttpBinsing, wsHttpBinding.
JQuery supports only HTTP bindings of WCF, bacause the browser can send/receive only Http (or xmlHttp) messages and JQuery is limited by browser technology. JQuery method $.ajax() supports only Http methods of GET, POST, PUT, DELETE.
GET and POST methods are the most frequently used methods.
GET request is sent via the URL string (appended to the URI with a question-mark as separator), which is visible whereas POST request is encapsulated in the body of the Http request and cannot be seen.
Since GET goes via URL, the maximum size of the GET request is limited to 255 bytes. There is no such limitation on POST request. So I prefer using POST method in all my code below. GET may also be used; both the ways are given below.
Since the web message format that we use is JSON (JSON is smaller than XML), for each operation contract in the service interface, we need to write
[OperationContract(Name = "GetMessages")]
[WebGet(UriTemplate = "/MessageId={messageId}",
ResponseFormat = WebMessageFormat.JSON,
RequestFormat = WebMessageFormat.JSON)]
Message GetMessages(int messageId);
OR specify different format of RESTful URI below
[OperationContract(Name = "GetMessages")]
[WebGet(UriTemplate = "/MessageId/{messageId}",
ResponseFormat = WebMessageFormat.JSON,
RequestFormat = WebMessageFormat.JSON)]
Message GetMessages(int messageId);
The WebGet attribute exposes the method to GET operations so that the method is directly accessible via a web browser by typing the URI on address bar of the browser and the parameters to the service method can be sent via querystring.
The WebInvoke attribute exposes the method to other Http operations like PUT, POST, DELETE
Both the above attributes have UriTemplate property to specify the format of the RESTful URI.
<serviceMetadata httpGetEnabled="true"> is added to the service config file to enable the user to view metadata through web browser and generate WSDL file.
Set includeExceptionDetailInFaults="true" allows the WCF service to throw original error (useful for debugging).
Add <webHttp/> to endpoint behavior and webHttpBinding to enable web programming model for WCF and allow the service to be accessible through web protocols (Http).
webHttpBinding
This is the ideal way of calling WCF service from JQuery. The name value pair of input parameter and value can be passed as part of the URL or as data parameter of $.ajax() method. However all the data passed can be seen as part of the URL, so it may not be always best for security considerations.
Below is the sample of the web.config for the WCF service.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
The command for invoking the service in JQuery is below.
$.ajax({
type: "GET",
url: "../Services/Dashboard.svc/GetMessages?MessageId=4", // the name value pair
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: false,
success: function(result) {
var messsage = result.d;
},
error: function() { alert("Error"); }
});
This is the same as below statement. The name value pair input is sent as data parameter when POST method is used.
$.ajax({
type: "POST",
url: "../Services/Dashboard.svc/GetMessages",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{ MessageId:" + 4 + "}", // the name value pair input
processData: false,
success: function(result) {
var messsage = result.d;
},
error: function() { alert("Error"); }
});
basicHttpBinding
This is the same as