现在的位置: 首页 > 综合 > 正文

XmlHttp调用WCF

2012年03月04日 ⁄ 综合 ⁄ 共 3486字 ⁄ 字号 评论关闭

 

  1 <html xmlns="http://www.w3.org/1999/xhtml" >
  2 <head id="Head1" runat="server">
  3     <title></title>
  4     <script language="javascript" type="text/javascript">
  5         function ajaxCall() {
  6             var xmlHttp;
  7             try {// Firefox, Opera 8.0+, Safari
  8                 xmlHttp = new XMLHttpRequest(); //实例化XMLHttpRequest对象
  9             }
 10             catch (e) {
 11 
 12                 // Internet Explorer 5+
 13                 try {
 14                     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 15                 }
 16                 catch (e) {
 17 
 18                     try {
 19                         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 20                     }
 21                     catch (e) {
 22                         alert("浏览器不支持AJAX!");
 23                         return false;
 24                     }
 25                 }
 26             }
 27             //绑定数据处理函数。
 28             xmlHttp.onreadystatechange = function() {
 29                 if (xmlHttp.readyState == 4) {
 30                     alert(xmlHttp.status);3
 31                     if (xmlHttp.status == 200) {
 32                         document.getElementById('lbText').innerHTML = xmlHttp.responseText;
 33                     }
 34                     else {
 35                         alert('请求出错.');
 36                     }
 37                 }
 38             }
 39             xmlHttp.open("Get""http://localhost:5765/WCFAJAX/AJAXWCF.svc/lx?id=lx"true); //异步请求数据
 40             xmlHttp.send(null); //strName为WebService方法HelloWorld中形参数名
 41 
 42         }
 43     
 44     </script>
 45 
 46 
 47 </head>
 48 <body>
 49     <form id="form1" runat="server">
 50     <div>
 51         <asp:Label ID="lbText" runat="server"></asp:Label>
 52         <a href="javascript:void(0)"  onclick='ajaxCall()'>点击此处</a>
 53     </div>
 54     </form>
 55 </body>
 56 </html>
 57 
 58 WCF 代码
 59 
 60 引用命名空间 using System.ServiceModel.Web;
 61 
 62 [OperationContract]
 63     [WebGet(UriTemplate="lx?id={strAjax}")]
 64   public  string DoWork(string strAjax);
 65 
 66 {
 67         return strAjax + "aa";
 68  }
 69 
 70 WebConfig 配置
 71 
 72 
 73 
 74  <system.serviceModel>
 75         <behaviors>
 76         <!--配置WebConfig 可使WCF可被客户端调用  -->
 77           <endpointBehaviors>       
 78             <behavior name="WCF">   
 79               <webHttp/>              
 80             </behavior>                         
 81           </endpointBehaviors>
 82           <!--配置WebConfig 可使WCF可被客户端调用-->
 83           <serviceBehaviors>
 84             
 85                 <behavior name="AJAXWCFBehavior">
 86                     <serviceMetadata httpGetEnabled="true" />
 87                     <serviceDebug includeExceptionDetailInFaults="false" />
 88                  
 89                 </behavior>
 90             </serviceBehaviors>
 91         </behaviors>
 92         <services>
 93             <service behaviorConfiguration="AJAXWCFBehavior" name="AJAXWCF">
 94               <!--配置WebConfig 可使WCF绑定webHttpBinding-->
 95               <endpoint behaviorConfiguration="WCF" contract="IAJAXWCF" address="" binding="webHttpBinding" />
 96               <!--配置WebConfig 可使WCF绑定webHttpBinding-->
 97               <!--BehaviorConfiguration: 对于webHttpBinding, 你需要将该属性设为一个拥有webHttp或enableWebScript子节点的终结点行为。-->
 98 
 99               <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
100             </service>
101         </services>
102     </system.serviceModel>
103 
104 

 

 

抱歉!评论已关闭.