本篇文章主要介紹了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