AJAX异步调用WebService
代码
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
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