Ajax interacts with WCF-json

Source: Internet
Author: User
Tags foreach json

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]
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.