JQuery Ajax调用WCF实例以及遇到的问题

1.遇到的最多的问题就是跨域问题,这个时间需要我们添加如下代码解决跨域的问题

第一步:在服务类加Attribute

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

第二步:在构造函数中添加

if (WebOperationContext.Current != null)
{
      WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
      WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

2.如何暴漏在WCF中使用的实体、枚举等的于客户端中

在服务接口中添加如下Attribute

[ServiceKnownType(typeof(类型))]

在服务接口的实现类中添加如下Attribute

[KnownType(typeof(类型))]

3.设置WCF服务方法返回的类型

[WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]

代码文件会在后续中陆续提供,敬请关注

联系我hornet_team@sina.com

posted @ 2014-11-29 13:29  hornet_team  阅读(260)  评论(0编辑  收藏  举报