AJAX异步调用WebService

 

ExpandedBlockStart.gif 代码
 1  < html xmlns = " http://www.w3.org/1999/xhtml "   >
 2  < head 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                       if  (xmlHttp.status  ==   200 ) {
31                          document.getElementById( ' lbText ' ).innerHTML  =  xmlHttp.responseText;
32                      }
33                       else  {
34                          alert( ' 请求出错. ' );
35                      }
36 
37 
38                  }
39              }
40              xmlHttp.open( " POST " " http://localhost:1576/WebService1.asmx/HelloWorld " true );  // 异步请求数据
41              xmlHttp.setRequestHeader( " Content-Type " " application/x-www-form-urlencoded; " );  // Post请求需设置请求RequestHeader否则xmlHttp.status为500 42 
43              xmlHttp.send( " strName=gg " );  // strName为WebService方法HelloWorld中形参数名
44 
45          }
46      
47       </ script >
48 
49 
50  </ head >
51  < body >
52       < form id = " form1 "  runat = " server " >
53       < div >
54           < asp:Label ID = " lbText "  runat = " server " ></ asp:Label >
55           < a href = " javascript:void(0) "   onclick = ' ajaxCall() ' > 点击此处 </ a >
56       </ div >
57       </ form >
58  </ body >
59  </ html >
60 
61 
62 
63  ======= WebService ==========
64 
65   [WebService(Namespace  =   " http://tempuri.org/ " )]
66      [WebServiceBinding(ConformsTo  =  WsiProfiles.BasicProfile1_1)]
67      [System.ComponentModel.ToolboxItem( false )]
68       //  若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
69       //  [System.Web.Script.Services.ScriptService]
70       public   class  WebService1 : System.Web.Services.WebService
71      {
72 
73          [WebMethod,ScriptMethod] // ScriptMethod 设置为可被客户端调用的WebService方法
74 
75           public   string  HelloWorld( string  strName)
76          {
77               return  strName + " Hello World " ;
78          }
79      }
80 
81 

 

 

转载于:https://www.cnblogs.com/zzandlx/archive/2010/11/17/1879754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值