[DataContract] public class City
{
int seq = 0;
string cityID;
string ctiyName;
[DataMember] public string CityID
{
get
{
return cityID;
}
set
{
cityID=value;
}
}
[DataMember] public string CityName
{
get { return ctiyName; }
set { ctiyName = value; }
}
[DataMember] public int Seq
{
get
{ return seq; }
set
{ seq = value; }
}
}
实现代码
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 : IService1
{
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
#region IService1 成员
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] public City GetDataUsingDataContract(City composite)
{
City c = new City();
c.CityID = composite.CityID;
c.CityName = composite.CityName;
c.Seq = composite.Seq;
return c;
}
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] public List<City> GetList()
{
List<City> list = new List<City>();
City cc = new City();
cc.CityID = "1";
cc.CityName="北京";
cc.Seq = 3;
list.Add(cc);
City cc1 = new City();
cc1.CityID = "2";
cc1.CityName = "上海";
cc1.Seq = 4;
list.Add(cc1);
return list;
}
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] public List<City> GetListData(List<City> list)
{
return list;
}
#endregion
}客户端调用代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WcfServiceDemoOne.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//参数为整数的方法
function fn1()
{
$.ajax({
url: "http://localhost:12079/Service1.svc/GetData",
type: "POST",
contentType: "text/json",
data: '{"value":2}',
dataType: "json",
success: function(returnValue) {
alert(returnValue);










