I want to use jquery ajax call to return json data from c# .net. Now since I want to return json, I am thinking of using WCF or using a page method as WebMethod.
From what I have tried using a page WebMethod i receive the following response "as" json:
{"d":"{\"ID\":1,\"Value\":\"First Value\"}"}
Is this a proper json, or is there a way to get a "clean" json using WCF?
ajax
$.ajax({
type: 'POST',
url: 'Service1.svc/DoWork',
cache: false,
contentType: "application/json; charset=utf-8",
data: "{ }",
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (xhr, msg, msg2) {
alert(msg);
}
});
WCF
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public List<TestClass> DoWork()
{
List<TestClass> tc = new List<TestClass>();
tc.Add(new TestClass() { ID = 1, Value = "First Value" });
return tc;
}
public class TestClass
{
public TestClass()
{ }
public int ID { get; set; }
public string Value { get; set; }
}
}