As most of you know, JQuery is becoming more popular, and it is very convinient to use. Below is an example demonstrates how to call WCF service using jQuery...
1, Create a ajax-enabled WCF service say "AJAXService.svc", you shall see below two key elements in your web config
<system.serviceModel>
<behaviors>
....
<endpointBehaviors>
<behavior name="AJAXServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
....
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="AJAXService">
<endpoint address="" behaviorConfiguration="AJAXServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="AJAXService" />
</service>
</services>
</system.serviceModel>
2, Put below javascript functions into the page you want use jQuery, this implies you will add the reference to jQuery library.
function callBack(result) {
alert(result.d);
}
function TestJQuery() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "AJAXService.svc/DoWork",
dataType: "json",
data: '{"ID":"1","Filter":"Gene"}',
processData: false,
success: callBack,
error: jqueryError
});
}
function jqueryError(request, status, error) {
document.getElementById("divError").innerHTML = request.responseText;
}
3, In the page, put a trigger for TestJQuery() function like below...
<input type="button" value="Test" onclick="TestJQuery()" />
4, You shall see a popup of the result returned by the WCF service.