最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery

来源:动视网 责编:小采 时间:2020-11-27 21:19:03
文档

jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery

jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery:不废话了,直奔主题吧 wcf端: 近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即: Factory=System.ServiceModel.A
推荐度:
导读jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery:不废话了,直奔主题吧 wcf端: 近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即: Factory=System.ServiceModel.A


不废话了,直奔主题吧

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调用代码:
代码如下:

$().ready(function () {


$.post("HelloWorld.svc/PostRestfulTest/111/222", function (data) {
alert("PostRestfulTest调用成功,返回值为:" + data);
})

$.get("HelloWorld.svc/GETRestfulTest/333/444", function (data) {
alert("GETRestfulTest调用成功,返回值为:" + data);
})

$.get("HelloWorld.svc/RestfulTest/555/666", function (data) {
alert("RestfulTest GET方式调用成功,返回值为:" + data);
})


$.post("HelloWorld.svc/RestfulTest/777/888", function (data) {
alert("RestfulTest POST方式调用成功,返回值为:" + data);
})


$.get("HelloWorld.svc/GETTest", { person: "aaa", welcome: "bbb" }, function (data) {
alert("GETTest 调用成功,返回值为:" + data);
});


$.ajax({
url: "HelloWorld.svc/PostTest",
type: "POST",
contentType: "application/json",
data: '{"person":"ccc","welcome":"ddd"}',
dataType: "html",
success: function (data) { alert("PostTest调用成功,返回值为:" + data); }
});
})


有时候,WCF暴露的方法中可能需要一些敏感信息做为参数(比如用户名/用户ID之类),这时如果直接用js来调用wcf,可能会把这部分信息泄漏在客户端,这种场景下,我们也经常用一个服务端的ashx来做中转

TestService.svc
代码如下:
using System.ServiceModel;

namespace ashx_jQuery
{
[ServiceContract]
public class TestService
{
///


/// 获取当前用户指定月份的工资
///

///
///
///
[OperationContract]
public double GetSalary(int userId,int month)
{
if (month == 1)//只是演示而已
{
return 5000;
}
else
{
return 1000;
}
}
}
}

AjaxProcess.ashx
代码如下:
using System.Web;

namespace ashx_jQuery
{
///


/// Summary description for AjaxProcess
///

public class AjaxProcess : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string month = context.Request["month"];

TestService wcf = new TestService();
double salary = wcf.GetSalary(GetUserId(), int.Parse(month));
context.Response.Write("{salary:" + salary + "}");
}


///


/// 获取当前的用户ID
///

///
private int GetUserId()
{
return 1;
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

jQuery调用:
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="ashx_jQuery._default" %>




jQuery ashx Sample


$().ready(function () {
$("#btnTest").click(function () {
$.post(
"AjaxProcess.ashx",
{ month:1 },
function (e) {
var d = eval("(" + e + ")");
alert(d.salary);
}, "html");
})
})







示例代码:点击下载

文档

jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery

jQuery调用RESTfulWCF示例代码(GET方法/POST方法)_jquery:不废话了,直奔主题吧 wcf端: 近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即: Factory=System.ServiceModel.A
推荐度:
标签: restful jQuery WCF
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top