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