There is a DataContractJsonSerializer formatter in WCF serialization, which makes WCF more of an input-output encoding format than the traditional XML Web Serice: JSON. This article describes how to use JSON to implement data interaction between Ajax and WCF.
The full name of JSON is JavaScript Object notation, a coded format that is designed to accommodate Ajax and restful instead of XML. It's better suited to JavaScript than XML,
Step one: Create a. Net Framework 3.5 Web site project in VS2008, named Ajaxwcfjson, not to mention, not to create a reference to the previous article Ajax and WCF interactive-WCF beauty,
Step two: Add an AJAX-enabled WCF service to your Web site project: wcfjsonservice.svc, change the code to read as follows:
[DataContract]
public class Person
{
[DataMember]
public string Name {get; set;}
[DataMember]
public string Address {get; set;}
}
[ServiceContract (Namespace = "")]
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
public class Wcfjsonservice
{
static list<person> List = new list<person> ();
Add the [WebGet] property to use the HTTP get
[OperationContract]
[WebInvoke (Requestformat = Webmessageformat.json)]
public bool Createperson (person P)
{
Add an action implementation here
foreach (person in list)
{
if (P.name ==person. Name)
{
return false;
}
}
List. ADD (P);
Trace.WriteLine ("-----------------------------------------");
foreach (person in list)
{
Trace.WriteLine ("Name:" + person.) Name + ', Address: ' + person. address);
}
Trace.WriteLine ("-----------------------------------------");
return true;
}
[OperationContract]
[WebInvoke (Responseformat=webmessageformat.json)]
Public person Getperson (string name)
{
foreach (person p in list)
{
if (p.name = = Name)
{
return p;
}
}
Return to new person ();
}
Add more actions here and mark them with [OperationContract]
}