欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實現(xiàn)ajax調(diào)用WCF服務(wù)的方法(附帶demo下載)

 更新時間:2015年12月04日 11:10:52   作者:游響云停  
這篇文章主要介紹了jQuery實現(xiàn)ajax調(diào)用WCF服務(wù)的方法,以完整實例形式分析了jQuery的ajax前端調(diào)用及后臺交互調(diào)用WCF服務(wù)的相關(guān)技巧,并附帶完整實例共讀者下載,需要的朋友可以參考下

本文實例講述了jQuery實現(xiàn)ajax調(diào)用WCF服務(wù)的方法。分享給大家供大家參考,具體如下:

關(guān)于AJAX調(diào)用WCF服務(wù)分為跨域和不跨域兩種方式,今天咱們先介紹下不跨域下的調(diào)用方法。DEMO是在VS2008寫的.

經(jīng)過測試與研究,發(fā)現(xiàn)AJAX調(diào)用WCF服務(wù)必須滿足以下條件

1.wcf的通訊方式必須使用webHttpBinding
2.必須設(shè)置<endpointBehaviors>節(jié)點的值
3.服務(wù)的實現(xiàn)必須添加標(biāo)記

復(fù)制代碼 代碼如下:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

4.方法前面必須添加如下標(biāo)記
復(fù)制代碼 代碼如下:
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]

5.ajax方法中傳遞的參數(shù)名稱必須和wcf服務(wù)中提供的參數(shù)方法名稱一致

以下是本人寫的代碼,標(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ù)信息,請在部署前將以下值設(shè)置為 false 并刪除上面的元數(shù)據(jù)終結(jié)點--> 
     <serviceMetadata httpGetEnabled="true"/> 
     <!-- 要接收故障異常詳細信息以進行調(diào)試,請將以下值設(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); 
 } 
 // 使用下面示例中說明的數(shù)據(jù)約定將復(fù)合類型添加到服務(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; } 
  } 
}

實現(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ù)為實體類的方法 
  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'); 
    } 
   }); 
  } 
//返回值為類集合的方法 
  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> 

完整實例代碼代碼點擊此處本站下載

希望本文所述對大家jQuery程序設(shè)計有所幫助。

相關(guān)文章

  • jquery 動態(tài)增加,減少input表單的簡單方法(必看)

    jquery 動態(tài)增加,減少input表單的簡單方法(必看)

    下面小編就為大家?guī)硪黄猨query 動態(tài)增加,減少input表單的簡單方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • 動態(tài)生成的DOM不會觸發(fā)onclick事件的原因及解決方法

    動態(tài)生成的DOM不會觸發(fā)onclick事件的原因及解決方法

    下面小編就為大家?guī)硪黄獎討B(tài)生成的DOM不會觸發(fā)onclick事件的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • jquery通過索引值操作html元素的代碼

    jquery通過索引值操作html元素的代碼

    這篇文章主要介紹了jquery通過索引值操作html元素的代碼,需要的朋友可以參考下
    2023-05-05
  • jQuery中:disabled選擇器用法實例

    jQuery中:disabled選擇器用法實例

    這篇文章主要介紹了jQuery中:disabled選擇器用法,實例分析了:disabled選擇器功能、定義及選取所有禁用的表單元素的技巧,需要的朋友可以參考下
    2015-01-01
  • jQuery實現(xiàn)下滑菜單導(dǎo)航效果代碼

    jQuery實現(xiàn)下滑菜單導(dǎo)航效果代碼

    這篇文章主要介紹了jQuery實現(xiàn)下滑菜單導(dǎo)航效果代碼,通過jquery操作鼠標(biāo)事件及頁面樣式動態(tài)操作實現(xiàn)下滑菜單導(dǎo)航功能,非常具有實用價值,需要的朋友可以參考下
    2015-08-08
  • jquery UI 1.72 之datepicker

    jquery UI 1.72 之datepicker

    一步一步學(xué)jquery UI 1.72 之datepicker,中間都寫了注釋方便大家學(xué)習(xí)。
    2009-12-12
  • toggle一個div顯示或隱藏且可擴展成自定義下拉框

    toggle一個div顯示或隱藏且可擴展成自定義下拉框

    本文為大家介紹下如何讓一個div顯示或隱藏且可擴展成自定義下拉框,具體實現(xiàn)如下,感興趣的朋友可參考下,希望對大家有所幫助
    2013-09-09
  • jquery中$.fn和圖片滾動效果實現(xiàn)的必備知識總結(jié)

    jquery中$.fn和圖片滾動效果實現(xiàn)的必備知識總結(jié)

    圖片滾動效果相信大家都使用過,看上去很簡單的一個效果,如果想熟練的掌握必須知道jquery、IIFE、setInterval等基礎(chǔ)以及$.fn用法,下面這篇文章主要介紹了關(guān)于jquery中$.fn和圖片滾動效果制作的必備知識,需要的朋友可以參考下。
    2017-04-04
  • jQuery操作Dom元素與遍歷以及JS遍歷詳細講解

    jQuery操作Dom元素與遍歷以及JS遍歷詳細講解

    這篇文章主要介紹了jQuery操作Dom元素、jQuery遍歷、JavaScript遍歷,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • jQuery 利用ztree實現(xiàn)樹形表格的實例代碼

    jQuery 利用ztree實現(xiàn)樹形表格的實例代碼

    最近公司要做一個樹形表格,由于之前對ztree實現(xiàn)基本的樹形結(jié)構(gòu),所以想到用ztree來做,下面小編給大家分享實現(xiàn)代碼,感興趣的朋友一起看看吧
    2017-09-09

最新評論