jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6808字,纯文字阅读大概需要10分钟。
内容图文
不废话了,直奔主题吧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中的
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<endpoint address="" behaviorConfiguration="ajaxSample.HelloWorldAspNetAjaxBehavior"
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
{
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
{
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
{
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
{
List
result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 只能Get的常规方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public List
{
List
result.Add("GETTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
}
}
jQuery调用代码:
内容总结
以上是互联网集市为您收集整理的jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery全部内容,希望文章能够帮你解决jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。