AJAX异步调用WebService

本文介绍了一种通过Ajax实现客户端与WebService交互的方法。具体展示了如何使用JavaScript创建XMLHttpRequest对象,并发送POST请求到指定的WebService接口。同时,文章还提供了WebService端的实现代码,包括必要的命名空间、绑定属性及方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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为50042 
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值