不废话了,直奔主题吧
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二种方式,下面把几种常用的情况都写一个示例方法:
001 |
using System.Collections.Generic; |
002 |
using System.ServiceModel; |
003 |
using System.ServiceModel.Activation; |
004 |
using System.ServiceModel.Web; |
009 |
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] |
010 |
public class HelloWorld |
016 |
/// <param name="person"></param> |
017 |
/// <param name="welcome"></param> |
018 |
/// <returns></returns> |
020 |
[WebInvoke(Method = "POST" , UriTemplate = "PostRestfulTest/{person}/{welcome}" , ResponseFormat = WebMessageFormat.Json)] |
021 |
public List< string > PostRestfulTest( string person, string welcome) |
023 |
List< string > result = new List< string >(); |
025 |
result.Add( "PostRestfulTest -> from server:" ); |
034 |
/// <param name="person"></param> |
035 |
/// <param name="welcome"></param> |
036 |
/// <returns></returns> |
038 |
[WebInvoke(Method = "GET" , UriTemplate = "GETRestfulTest/{person}/{welcome}" , ResponseFormat = WebMessageFormat.Json)] |
039 |
public List< string > GETRestfulTest( string person, string welcome) |
041 |
List< string > result = new List< string >(); |
043 |
result.Add( "GETRestfulTest -> from server:" ); |
050 |
/// 即可Get与Post的Restful方法 |
052 |
/// <param name="person"></param> |
053 |
/// <param name="welcome"></param> |
054 |
/// <returns></returns> |
056 |
[WebInvoke(Method = "*" , UriTemplate = "RestfulTest/{person}/{welcome}" , ResponseFormat = WebMessageFormat.Json)] |
057 |
public List< string > RestfulTest( string person, string welcome) |
059 |
List< string > result = new List< string >(); |
061 |
result.Add( "RestfulTest -> from server:" ); |
069 |
/// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped) |
071 |
/// <param name="person"></param> |
072 |
/// <param name="welcome"></param> |
073 |
/// <returns></returns> |
075 |
[WebInvoke(Method = "POST" , ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)] |
076 |
public List< string > PostTest( string person, string welcome) |
078 |
List< string > result = new List< string >(); |
080 |
result.Add( "PostRestfulTest -> from server:" ); |
089 |
/// <param name="person"></param> |
090 |
/// <param name="welcome"></param> |
091 |
/// <returns></returns> |
093 |
[WebInvoke(Method = "GET" , ResponseFormat = WebMessageFormat.Json)] |
094 |
public List< string > GETTest( string person, string welcome) |
096 |
List< string > result = new List< string >(); |
098 |
result.Add( "GETTest -> from server:" ); |
jQuery调用代码:
01 |
<script type= "text/javascript" > |
02 |
$().ready( function () { |
05 |
$.post( "HelloWorld.svc/PostRestfulTest/111/222" , function (data) { |
06 |
alert( "PostRestfulTest调用成功,返回值为:" + data); |
09 |
$.get( "HelloWorld.svc/GETRestfulTest/333/444" , function (data) { |
10 |
alert( "GETRestfulTest调用成功,返回值为:" + data); |
13 |
$.get( "HelloWorld.svc/RestfulTest/555/666" , function (data) { |
14 |
alert( "RestfulTest GET方式调用成功,返回值为:" + data); |
18 |
$.post( "HelloWorld.svc/RestfulTest/777/888" , function (data) { |
19 |
alert( "RestfulTest POST方式调用成功,返回值为:" + data); |
23 |
$.get( "HelloWorld.svc/GETTest" , { person: "aaa" , welcome: "bbb" }, function (data) { |
24 |
alert( "GETTest 调用成功,返回值为:" + data); |
29 |
url: "HelloWorld.svc/PostTest" , |
31 |
contentType: "application/json" , |
32 |
data: '{"person":"ccc","welcome":"ddd"}' , |
34 |
success: function (data) { alert( "PostTest调用成功,返回值为:" + data); } |