Call WCF service method from javascript (jQuery)

By | October 12, 2013

Advertisement from Google

In Advanced Tic Tac Toe we have created WCF backend service and web-based frontend application. Our web application written in javascript, so we want to call WCF service methods through jQuery.

There is a small instruction how-to call WCF service from javascript code.

First of all you should decorate your service interface with WebInvoke attribute

[WebInvoke(UriTemplate = "Start",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           Method = "POST",
           BodyStyle = WebMessageBodyStyle.Wrapped)]
ComplexType Start(Guid gameId, ComplexType2 type);

Then you should add following endpoint to your web/application.config:

Advertisement from Google

<endpoint address="/rest" binding="webHttpBinding" contract="TicTacToe.Service.Interfaces.ITicTacToeService" behaviorConfiguration="restBehavior" />
   <behavior name="restBehavior">
    <webHttp />

Now, if yout service is available through address your could call your method by url.

To invoke this method you should use following jQuery ajax method call:

	url: "Service.svc/rest/Start",
	type: "POST",
	dataType: "json",
	contentType: "application/json",
	data: JSON.stringify({ gameId: "{3ecb9d7b-f5a2-4a3e-b59f-896391d77829}",  type: {ComplexType2Prop: 100500}},
	success: function(result) {
		var test = result.StartResult.ComplexTypeProperty;

Thats all, thanks 😉

Leave a Reply

Your email address will not be published. Required fields are marked *