1.在站点先新建一个wcf web服务, Service.svc文件内容如下:
Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
注意要自己加上绿色部分
2.修改web.config把wcf的配置节点修改成如下形式
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<!--<enableWebScript />-->
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="Service" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="Service" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
要注意绿色部分首先注释掉默认生产的<enableWebScript />,然后是加入 httpGetEnabled="true"
3. 修改App_Code下Service.cs内容如下
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Collections.Generic;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
[WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
public Person DoWork1(Person p1)
{
return p1;
}
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped)]
public Person DoWork2(string name ,int age)
{
return new Person { Name = name, Age = age };
}
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public List<Person> DoWork3(Person p1,Person p2)
{
List<Person> list = new List<Person>();
list.Add(p1);
list.Add(p2);
return list;
}
}
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age {get;set;}
}
4,新建一个asp.net页面default.aspx,在页面的head部分加入如下内容json2.js是一个用来在json对象和字符串之间转换的js库文件
json2.js下载地址 http://www.json.org/json2.js
<script src="json2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function() {
//调用DoWork1并传递一个json格式的Person对象,返回结果是一个json格式对象
var data = { Name: "Denny", Age: 23 };
var jsonStr = JSON.stringify(data); //将对象格式化成json字符串
sendAJAX("Service.svc/Dowork1", jsonStr,
function(msg) {
var result = JSON.parse(msg); //将json字符串反序列化成js对象
alert(result.Name +" "+ result.Age);
});
//调用DoWork2传入两个参数name和age 返回一个包装过的Json对象
data = { name: "Denny", age: 23 };
jsonStr = JSON.stringify(data);
sendAJAX("Service.svc/Dowork2", jsonStr,
function(msg) {
var result = JSON.parse(msg);
alert(result.DoWork2Result.Name + " " + result.DoWork2Result.Age);
});
//调用DoWork3传入两个Person对象,返回俩个Person对象
data = { p1: { Name: "Denny", Age: 23 }, p2: { Name: "Denny", Age: 30} };
jsonStr = JSON.stringify(data);
sendAJAX("Service.svc/Dowork3", jsonStr,
function(msg) {
var result = JSON.parse(msg);
alert(result[0].Name + " " + result[0].Age +"---"+ result[1].Name + " " + result[1].Age);
});
});
function sendAJAX(url,data,success) {
$.ajax({
type: "POST",
contentType: "application/json",
url: url,
data: data,
processData: false,
success: success,
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error Occured!");
}
});
}
</script>
ok,目前完成了~下面解释下wcf方法上面的attribute
[WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
这三个比较容易理解。Method="POST",客户端必须使用post方法来调用,RequestFormat = WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json是说参数和返回值的格式都应该是json格式。
下面说说 BodyStyle 参数
这个参数是个枚举包括如下值
WebMessageBodyStyle.Bare
WebMessageBodyStyle.Wrapped
WebMessageBodyStyle.WrappedRequest
WebMessageBodyStyle.WrappedResponse
Bare是个默认设置,第一个方法dowork1 就是bare.官方定义我也搞不清楚。我的理解是在bare的情况下客户端传来的json对象参数,会被服务器
当成一个参数来对待。所以bare情况下服务器方法不能有多个参数。
bare情况下的返回值很容易理解的。很直接就是你想要的对象
Warpped是参数和返回值都要经过包装,比如参数是Person p,你要传递json参数应该是{p:{"Name":"aa","Age":22}},如果返回值是一个Person,则json格式是
{"DoWork2Result":{"Name":"aaa","Age":22}}。wcf会把返回的对象包装到另个一个对象下面的一个属性上。属性名是方法名+Result。所以为了方便最好是吧返回值设置成
Bare的。但是要传递多个参数时候必须把参数格式设置成Wapped。比如第二个和第三个方法就是传递了两个参数。注意json里的属性名和wcf方法参数名保持一样。
后面两个值WrappedRequest和WebMessageBodyStyle.WrappedResponse是单独设置参数格式和返回格式的。两个都指定就等于WebMessageBodyStyle.Wrapped
终于完了!赶紧干活,不能加班!
参考: http://www.cnblogs.com/binglingshui/archive/2008/12/29/1364647.html