视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery
2020-11-27 21:19:03 责编:小采
文档

不废话了,直奔主题吧

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中的即类似:





-->



multipleSiteBindingsEnabled="true" />


binding="webHttpBinding" contract="ajaxSample.HelloWorld" />


好了,开始写代码,鉴于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
{

///


/// 只能Post的Restful方法
///

///
///
///
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List PostRestfulTest(string person,string welcome)
{
List result = new List();

result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}

///


/// 只能Get的Restful方法
///

///
///
///
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List GETRestfulTest(string person, string welcome)
{
List result = new List();

result.Add("GETRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}

///


/// 即可Get与Post的Restful方法
///

///
///
///
[OperationContract]
[WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List RestfulTest(string person, string welcome)
{
List result = new List();

result.Add("RestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}


///


/// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped)
///

///
///
///
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
public List PostTest(string person, string welcome)
{
List result = new List();

result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}

///


/// 只能Get的常规方法
///

///
///
///
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public List GETTest(string person, string welcome)
{
List result = new List();

result.Add("GETTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}


}
}

jQuery调用代码:
代码如下:




示例代码:点击下载

下载本文
显示全文
专题