jQuery實(shí)現(xiàn)ajax調(diào)用WCF服務(wù)的方法(附帶demo下載)
本文實(shí)例講述了jQuery實(shí)現(xiàn)ajax調(diào)用WCF服務(wù)的方法。分享給大家供大家參考,具體如下:
關(guān)于AJAX調(diào)用WCF服務(wù)分為跨域和不跨域兩種方式,今天咱們先介紹下不跨域下的調(diào)用方法。DEMO是在VS2008寫(xiě)的.
經(jīng)過(guò)測(cè)試與研究,發(fā)現(xiàn)AJAX調(diào)用WCF服務(wù)必須滿足以下條件
1.wcf的通訊方式必須使用webHttpBinding
2.必須設(shè)置<endpointBehaviors>節(jié)點(diǎn)的值
3.服務(wù)的實(shí)現(xiàn)必須添加標(biāo)記
4.方法前面必須添加如下標(biāo)記
5.ajax方法中傳遞的參數(shù)名稱(chēng)必須和wcf服務(wù)中提供的參數(shù)方法名稱(chēng)一致
以下是本人寫(xiě)的代碼,標(biāo)記顏色的是需要注意的地方
服務(wù)器端配置文件代碼
<system.serviceModel>
<services>
<service name="WcfServiceDemoOne.Service1" behaviorConfiguration="WcfServiceDemoOne.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="webHttpBinding" contract="WcfServiceDemoOne.IService1" behaviorConfiguration="HttpBehavior"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:12079/Service1.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceDemoOne.Service1Behavior">
<!-- 為避免泄漏元數(shù)據(jù)信息,請(qǐng)?jiān)诓渴鹎皩⒁韵轮翟O(shè)置為 false 并刪除上面的元數(shù)據(jù)終結(jié)點(diǎn)-->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障異常詳細(xì)信息以進(jìn)行調(diào)試,請(qǐng)將以下值設(shè)置為 true。在部署前設(shè)置為 false 以避免泄漏異常信息-->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="HttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
服務(wù)器端代碼
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
City GetDataUsingDataContract(City composite);
[OperationContract]
List<City> GetList();
[OperationContract]
List<City> GetListData(List<City> list);
}
// 使用下面示例中說(shuō)明的數(shù)據(jù)約定將復(fù)合類(lèi)型添加到服務(wù)操作。
[DataContract]
public class City
{
int seq = 0;
string cityID;
string ctiyName;
[DataMember]
public string CityID
{
get
{
return cityID;
}
set
{
cityID=value;
}
}
[DataMember]
public string CityName
{
get { return ctiyName; }
set { ctiyName = value; }
}
[DataMember]
public int Seq
{
get
{ return seq; }
set
{ seq = value; }
}
}
實(shí)現(xiàn)代碼
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
#region IService1 成員
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public City GetDataUsingDataContract(City composite)
{
City c = new City();
c.CityID = composite.CityID;
c.CityName = composite.CityName;
c.Seq = composite.Seq;
return c;
}
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public List<City> GetList()
{
List<City> list = new List<City>();
City cc = new City();
cc.CityID = "1";
cc.CityName="北京";
cc.Seq = 3;
list.Add(cc);
City cc1 = new City();
cc1.CityID = "2";
cc1.CityName = "上海";
cc1.Seq = 4;
list.Add(cc1);
return list;
}
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public List<City> GetListData(List<City> list)
{
return list;
}
#endregion
}
客戶端調(diào)用代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WcfServiceDemoOne.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//參數(shù)為整數(shù)的方法
function fn1()
{
$.ajax({
url: "http://localhost:12079/Service1.svc/GetData",
type: "POST",
contentType: "text/json",
data: '{"value":2}',
dataType: "json",
success: function(returnValue) {
alert(returnValue);
},
error: function() {
alert('error');
}
});
}
//參數(shù)為實(shí)體類(lèi)的方法
function fn2() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetDataUsingDataContract",
type: "POST",
contentType: "application/json",
data: '{"CityID":1,"CityName":"北京","Seq":"3"}',
dataType: "json",
success: function(returnValue) {
alert(returnValue.CityID + ' ' + returnValue.CityName + "--" + returnValue.Seq);
},
error: function() {
alert('error');
}
});
}
//返回值為類(lèi)集合的方法
function fn3() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetList",
type: "POST",
contentType: "application/json",
dataType: "json",
success: function(returnValue) {
for (var i = 0; i < returnValue.length; i++) {
alert(returnValue[i].CityID + ' ' + returnValue[i].CityName+'---'+returnValue[i].Seq);
}
},
error: function() {
alert('error');
}
});
}
function fn4() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetListData",
type: "POST",
contentType: "application/json",
data: '[{"CityID":1,"CityName":"北京","Seq":"3"},{"CityID":3,"CityName":"上海","Seq":"3"}]',
dataType: "json",
success: function(returnValue) {
for (var i = 0; i < returnValue.length; i++) {
alert(returnValue[i].CityID + ' ' + returnValue[i].CityName + '---' + returnValue[i].Seq);
}
},
error: function() {
alert('error');
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="調(diào)用1" onclick="fn1();" /></div>
<input id="Button2" type="button" value="調(diào)用2" onclick="fn2();" />
<br />
<input id="Button3" type="button" value="調(diào)用3" onclick="fn3();" /></form>
<br />
<input id="Button4" type="button" value="調(diào)用4" onclick="fn4();"/>
</body>
</html>
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- C# 一個(gè)WCF簡(jiǎn)單實(shí)例
- 關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹
- C# WCF簡(jiǎn)單入門(mén)圖文教程(VS2010版)
- C# yield在WCF中的錯(cuò)誤用法(一)
- C# yield在WCF中的錯(cuò)誤使用(二)
- jQuery ajax調(diào)用WCF服務(wù)實(shí)例
- jQuery Ajax調(diào)用WCF服務(wù)詳細(xì)教程
- jquery調(diào)用wcf并展示出數(shù)據(jù)的方法
- WinForm窗體調(diào)用WCF服務(wù)窗體卡死問(wèn)題
- 總結(jié)C#動(dòng)態(tài)調(diào)用WCF接口的兩種方法
相關(guān)文章
可輸入文字查找ajax下拉框控件 ComBox的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇可輸入文字查找ajax下拉框控件 ComBox的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
淺談jquery采用attr修改form表單enctype不起作用的問(wèn)題
下面小編就為大家?guī)?lái)一篇淺談jquery采用attr修改form表單enctype不起作用的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
JQuery Ajax 異步操作之動(dòng)態(tài)添加節(jié)點(diǎn)功能
異步操作動(dòng)態(tài)添加節(jié)點(diǎn),導(dǎo)致在代碼中給添加的節(jié)點(diǎn)全局綁定事件或者獲取元素?zé)o效,問(wèn)題出在哪里呢?下文給大家介紹下,感興趣的朋友參考下吧2017-05-05
用jQuery簡(jiǎn)化Ajax開(kāi)發(fā)實(shí)現(xiàn)方法
使用 jQuery 將使 Ajax 變得及其簡(jiǎn)單。jQuery 提供有一些函數(shù),可以使簡(jiǎn)單的工作變得更加簡(jiǎn)單,復(fù)雜的工作變得不再?gòu)?fù)雜。2010-04-04
jQuery插件HighCharts繪制的2D堆柱狀圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制的2D堆柱狀圖效果,結(jié)合完整實(shí)例形式分析了jQuery插件HighCharts繪制2D柱狀圖的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
Ajax異步獲取html數(shù)據(jù)中包含js方法無(wú)效的解決方法
本文主要介紹了Ajax異步獲取html數(shù)據(jù)中包含js方法無(wú)效的解決方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
jQuery實(shí)現(xiàn)的分頁(yè)功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的分頁(yè)功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery實(shí)現(xiàn)分頁(yè)功能的具體步驟及相關(guān)操作技巧,包括前臺(tái)樣式、布局及jQuery分頁(yè)插件的調(diào)用方法,需要的朋友可以參考下2017-01-01
jQuery中將json數(shù)據(jù)顯示到頁(yè)面表格的方法
今天小編就為大家分享一篇jQuery中將json數(shù)據(jù)顯示到頁(yè)面表格的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼
jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10

