Geeks With Blogs
.Nettuce Code Salad
I can't believe how long this took to figure out, I think I read every WCF tutorial out there, and now I look at it it's so mind bogglingly simple I don't know what I wasted my time doing! All I wanted was a WCF service that would accept a POST from jQuery with some JSON parameters and return some HTML, but it seems I repeatedly cocked up the JSON string format and the attributes for the contract. Anyway, here's a simple example so you don't have to endure my frustration. I used .NET 4.0 because it's .svc free.


<?xml version="1.0"?>
<compilation debug="true" targetFrameworkMoniker=".NETFramework,Version=v4.0" />
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<add relativeAddress="Service" service="Service" />
<service name="Service">
<endpoint behaviorConfiguration="webHttpBehaviour" binding="webHttpBinding" contract="Service" />
<behavior name="webHttpBehaviour">


using System.IO;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;

[ServiceContract, AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
[OperationContract, WebInvoke(BodyStyle
= WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json)]
public Stream Post(string message, int x, int y)
return new MemoryStream(Encoding.UTF8.GetBytes("<b>" + message + (x + y) + "</b>"));


<script type="text/javascript" src=""></script>
<script type="text/javascript">
'{"x":"1","y":"2","message":"The answer is: "}',
function(data) { $('body').html(data); }
Posted on Sunday, October 18, 2009 11:18 PM | Back to top

Comments on this post: WCF jQuery REST JSON Service

# re: WCF jQuery REST JSON Service
Requesting Gravatar...
I have followed the steps to resolving cross domain issue using jsonp on rest wcf service.
This is my function in wcf service
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
res[] GetBooth(res ss);

public class res
public string title { get; set; }

public string desc { get; set; }

public res[] GetData(res val ) {
List<res> ress = new List<res>();
return ss.title;
return ress.ToArray();
Now I want to pass function parameter while calling from client as class objects.
function GetData() {

var reqParams = {
title: "hello"
var reqParams_Serialized = Sys.Serialization.JavaScriptSerializer.serialize(reqParams);
type: "Post",
cache: false,
url: "http://localhost:50933/test.svc/jsonp/getdata ",
scriptCharset: "utf-8",
contentType: 'application/json;charset=utf-8',
dataType: "json",
data: reqParams,
success: function(data, textStatus) {

error: function(XMLHttpRequest, textStatus, errorThrown) {

But when pass parameters like this it is not working.
Please suggest me something with example or modification in this code.

Parveen Punia

Left by parveen punia on Apr 08, 2010 2:58 PM

# re: WCF jQuery REST JSON Service
Requesting Gravatar...
Thanks, after hours of searching this post has finally given me the answer I need.
Left by Tim on Jun 06, 2010 9:13 AM

# re: WCF jQuery REST JSON Service
Requesting Gravatar...
Cool article,

I wrote a post which extends what you've shown in yours up on my blog:
Left by Yoav on Aug 31, 2010 3:14 PM

# re: WCF jQuery REST JSON Service
Requesting Gravatar...
Thanks a lot!
It's really works!
I searched it so long.
Left by Lion from USSR on Oct 22, 2012 10:22 AM

Your comment:
 (will show your gravatar)

Copyright © Jon Canning | Powered by: | Join free