您的位置:首页 > Web前端 > JavaScript

VS2008中JavaScript调用WebServices

2009-10-16 15:31 274 查看
最近这几天任务完成了,也没什么重要的事情,抽空学习了一下WebServices的知识,感觉还是挺有意思,难度也不是很大。
首先,用VS2008创建一个asp.net网站
其次,项目 右键—>添加新项—>Web 服务 如下图:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script language="javascript">
function Method(obj)
{
document.getElementById("txtSum").value = obj;
}

function Hello()
{
WebService.HelloWorld(backMethod);
}

function getSum()
{
var a,b;
a = document.getElementById("txtA").value;
b = document.getElementById("txtB").value;
try
{
WebService.GetSum(a, b, Method);
}
catch(err)
{
alert(err.description);
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="True" Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" id="btHello" value="Hello" onclick="Hello();" /><br />
<input type="text" id="txtA" value="" />+
<input type="text" id="txtB" value="" />=
<input type="text" id="txtSum" value="" />
<input type="button" id="btSum" value="求和" onclick="getSum();" /><br />
</div>
</form>
</body>
</html>
通过以上方法就可以轻松的调用WebService中的方法,WebService中也可以返回一个DataSet结果集。
后面还得继续学习WebService的知识。
如果大家有好的WebService学习的资料或者是网站的话,拿出来分享一下,以方便大家共同学习、交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: