1.在站点先新建一个wcf web服务, Service.svc文件内容如下:

<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" 

Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

注意要自己加上绿色部分

2.修改web.config把wcf的配置节点修改成如下形式

jquery使用json格式 调用 restful wcf web service<system.serviceModel>
jquery使用json格式 调用 restful wcf web service  
<behaviors>
jquery使用json格式 调用 restful wcf web service   
<endpointBehaviors>
jquery使用json格式 调用 restful wcf web service    
<behavior name="ServiceAspNetAjaxBehavior">
jquery使用json格式 调用 restful wcf web service     
<!--<enableWebScript />-->
jquery使用json格式 调用 restful wcf web service    
</behavior>
jquery使用json格式 调用 restful wcf web service   
</endpointBehaviors>
jquery使用json格式 调用 restful wcf web service    
<serviceBehaviors>
jquery使用json格式 调用 restful wcf web service      
<behavior name="ServiceAspNetAjaxBehavior">
jquery使用json格式 调用 restful wcf web service        
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
jquery使用json格式 调用 restful wcf web service        
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
jquery使用json格式 调用 restful wcf web service      
</behavior>
jquery使用json格式 调用 restful wcf web service    
</serviceBehaviors>
jquery使用json格式 调用 restful wcf web service  
</behaviors>
jquery使用json格式 调用 restful wcf web service  
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
jquery使用json格式 调用 restful wcf web service  
<services>
jquery使用json格式 调用 restful wcf web service   
<service name="Service" behaviorConfiguration="ServiceAspNetAjaxBehavior">
jquery使用json格式 调用 restful wcf web service     
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
jquery使用json格式 调用 restful wcf web service    binding
="webHttpBinding" contract="Service" />
jquery使用json格式 调用 restful wcf web service     
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
jquery使用json格式 调用 restful wcf web service   
</service>
jquery使用json格式 调用 restful wcf web service  
</services>
jquery使用json格式 调用 restful wcf web service 
</system.serviceModel>

 要注意绿色部分首先注释掉默认生产的<enableWebScript />,然后是加入 httpGetEnabled="true"

3. 修改App_Code下Service.cs内容如下

;}
}

 

jquery使用json格式 调用 restful wcf web serviceusing System;
jquery使用json格式 调用 restful wcf web service
using System.Linq;
jquery使用json格式 调用 restful wcf web service
using System.Runtime.Serialization;
jquery使用json格式 调用 restful wcf web service
using System.ServiceModel;
jquery使用json格式 调用 restful wcf web service
using System.ServiceModel.Activation;
jquery使用json格式 调用 restful wcf web service
using System.ServiceModel.Web;
jquery使用json格式 调用 restful wcf web service
using System.Collections.Generic;
jquery使用json格式 调用 restful wcf web service[ServiceContract(Namespace 
= "")]
jquery使用json格式 调用 restful wcf web service[AspNetCompatibilityRequirements(RequirementsMode 
= AspNetCompatibilityRequirementsMode.Allowed)]
jquery使用json格式 调用 restful wcf web service
public class Service
}

  4,新建一个asp.net页面default.aspx,在页面的head部分加入如下内容json2.js是一个用来在json对象和字符串之间转换的js库文件

json2.js下载地址 http://www.json.org/json2.js

jquery使用json格式 调用 restful wcf web service    <script src="jquery.js" type="text/javascript"></script>
jquery使用json格式 调用 restful wcf web service    
<script src="json2.js" type="text/javascript"></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格式是

{"

Bare的。但是要传递多个参数时候必须把参数格式设置成Wapped。比如第二个和第三个方法就是传递了两个参数。注意json里的属性名和wcf方法参数名保持一样。

 后面两个值WrappedRequest和WebMessageBodyStyle.WrappedResponse是单独设置参数格式和返回格式的。两个都指定就等于WebMessageBodyStyle.Wrapped

 

终于完了!赶紧干活,不能加班!

参考: http://www.cnblogs.com/binglingshui/archive/2008/12/29/1364647.html

相关文章:

  • 2021-11-16
  • 2022-02-13
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2021-10-17
  • 2021-12-29
  • 2021-08-19
猜你喜欢
  • 2021-05-19
  • 2021-11-19
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案