使用Ajax访问WebService示例

本文介绍了一个简单的ASP.NET WebService示例,通过C#实现加法操作,并利用AJAX技术从客户端调用该WebService进行远程计算。文章详细展示了WebService的实现代码以及测试页面的HTML和JavaScript代码。

 

WebService代码(WebService.asmx):

ContractedBlock.gifExpandedBlockStart.gifCode
 1<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
 2
 3using System;
 4using System.Web;
 5using System.Collections;
 6using System.Web.Services;
 7using System.Web.Services.Protocols;
 8
 9
10ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
11/// WebService 的摘要说明
12/// </summary>

13[WebService(Namespace = "http://tempuri.org/")]
14[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15ExpandedBlockStart.gifContractedBlock.gifpublic class WebService : System.Web.Services.WebService {
16
17    [WebMethod]
18    public int Sum(int a, int b)
19ExpandedSubBlockStart.gifContractedSubBlock.gif    {
20        return a + b;
21    }

22    
23}

 

 

测试页面代码(Test.html):

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2<html xmlns="http://www.w3.org/1999/xhtml" >
 3<head>
 4    <title>无标题页</title>
 5ExpandedBlockStart.gifContractedBlock.gif    <script type="text/javascript">
 6    var xmlhttp;
 7    function createXMLHttp()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 9        if(window.XMLHttpRequest)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        {
11            xmlhttp=new XMLHttpRequest();//Firefox、Opera
12        }

13        else if(window.ActiveXObject)    //IE两种申明方式
14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
15            try
16ExpandedSubBlockStart.gifContractedSubBlock.gif            {
17                xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
18            }

19            catch(e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif            {
21                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
22            }

23        }

24        if(!xmlhttp)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
26            window.alert("Your broswer not support XMLHttpRequest!");
27        }

28        return xmlhttp;
29    }

30    
31    function SumIt()
32ExpandedSubBlockStart.gifContractedSubBlock.gif    {
33        createXMLHttp();
34        var url="http://localhost/WebService.asmx/Sum";
35        var queryString=createQueryString();
36        xmlhttp.open("POST",url,true);
37        xmlhttp.onreadystatechange=handleStateChange;
38        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
39        xmlhttp.send(queryString);
40    }

41    
42    function createQueryString()
43ExpandedSubBlockStart.gifContractedSubBlock.gif    {
44        var a=document.getElementById("a").value;
45        var b=document.getElementById("b").value;
46        var quertyString="a="+a+"&b="+b;
47        return quertyString;
48    }

49    
50    
51    function handleStateChange()
52ExpandedSubBlockStart.gifContractedSubBlock.gif    {
53        if(xmlhttp.readyState==4)
54ExpandedSubBlockStart.gifContractedSubBlock.gif        {
55            if(xmlhttp.status==200)
56ExpandedSubBlockStart.gifContractedSubBlock.gif            {
57                displayResult();
58            }

59        }

60    }

61    
62    function displayResult()
63ExpandedSubBlockStart.gifContractedSubBlock.gif    {
64        var result=document.getElementById("result");
65        result.innerText="计算结果: "+xmlhttp.responseXML.getElementsByTagName("int")[0].firstChild.data;
66    }

67    
68    
69    
</script>
70</head>
71<body>
72<p>
73    <input type="text" id="a"/>
74</p>
75<p>
76    <input type="text" id="b"/>
77</p>
78<p>
79    <input type="button" name="Submit" onclick="SumIt();" value="计算"/>
80</p>
81<label id="result"></label>
82
83</body>
84</html>
85

 

     程序很简单:SumIt方法向服务器发请求;createQueryString将输入的内容组成查询字符串供POST使用;XMLHttp的状态改变的句柄设置到handleStateChange方法中;displayResult显示计算结果。其实AJAX的关键就是如何使用好各种浏览器都支持的XMLHttpRequest对象。

 

转载于:https://www.cnblogs.com/jiangjiancq/archive/2008/12/23/1360390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值