您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

jQuery调用RESTful WCF示例代码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 本篇文章主要介绍了jQuery调用RESTful WCF示例代码(GET方法/POST方法),需要的朋友可以过来参考下,希望对大家有所帮助

不废话了,直奔主题吧   wcf端:   近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即:   <%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>   注:如果不添加Factory,则wcf将无法用类似http://localhost/helloWorld.svc/Hello/person/name 的restful方式直接访问。   同时还要去掉web.config中的<enableWebScript />即类似:   <system.serviceModel>     <behaviors>       <endpointBehaviors>         <behavior name="ajaxSample.HelloWorldAspNetAjaxBehavior">           <!--<enableWebScript />-->         </behavior>       </endpointBehaviors>     </behaviors>     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"       multipleSiteBindingsEnabled="true" />     <services>       <service name="ajaxSample.HelloWorld">         <endpoint address="" behaviorConfiguration="ajaxSample.HelloWorldAspNetAjaxBehavior"           binding="webHttpBinding" contract="ajaxSample.HelloWorld" />       </service>     </services>   </system.serviceModel>   好了,开始写代码,鉴于wcf调用时有GET/POST二种方式,下面把几种常用的情况都写一个示例方法:   代码如下: using System.Collections.Generic;  using System.ServiceModel;  using System.ServiceModel.Activation;  using System.ServiceModel.Web;    namespace ajaxSample  {      [ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]      public class HelloWorld      {            /// <summary>          /// 只能Post的Restful方法          /// </summary>          /// <param name="person"></param>          /// <param name="welcome"></param>          /// <returns></returns>          [OperationContract]          [WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]          public List<string> PostRestfulTest(string person,string welcome)          {              List<string> result = new List<string>();                result.Add("PostRestfulTest -> from server:");              result.Add(person);              result.Add(welcome);              return result;          }            /// <summary>          /// 只能Get的Restful方法          /// </summary>          /// <param name="person"></param>          /// <param name="welcome"></param>          /// <returns></returns>          [OperationContract]          [WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]          public List<string> GETRestfulTest(string person, string welcome)          {              List<string> result = new List<string>();                result.Add("GETRestfulTest -> from server:");              result.Add(person);              result.Add(welcome);              return result;          }            /// <summary>          /// 即可Get与Post的Restful方法          /// </summary>          /// <param name="person"></param>          /// <param name="welcome"></param>          /// <returns></returns>          [OperationContract]          [WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]          public List<string> RestfulTest(string person, string welcome)          {              List<string> result = new List<string>();                result.Add("RestfulTest -> from server:");              result.Add(person);              result.Add(welcome);              return result;          }               /// <summary>          /// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped)          /// </summary>          /// <param name="person"></param>          /// <param name="welcome"></param>          /// <returns></returns>          [OperationContract]          [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]          public List<string> PostTest(string person, string welcome)          {              List<string> result = new List<string>();                result.Add("PostRestfulTest -> from server:");              result.Add(person);              result.Add(welcome);              return result;          }            /// <summary>          /// 只能Get的常规方法          /// </summary>          /// <param name="person"></param>          /// <param name="welcome"></param>          /// <returns></returns>          [OperationContract