javascript调用webservice
转载,原文地址:http://www.lhliving.cn/html/39-1616192-1.htm
.net 下用javascript调用webservice的话,要用到webservice behavior。下面以一个例子讲解之,比较简单
1 、首先,要创建一个webservice,比如
<%@ WebService Language="C#" class=MyMath %>2
using System;3
using System.Web.Services;4
public class MyMath 5
{6
[WebMethod]7
public int add(int a, int b)8
{9
return a + b;10
}11
[WebMethod]12
public int subtract(int a, int b)13
{14
return a - b;15
}16
}17

18

然后发布,先得到其wsdl。
2、首先,我们要下载webbehavior.htc这个文件(可以到http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp.)
去下载,然后放到你的web当前目录下然后在要调用webserice的页面中,修改如下
<body>
<div id="addservice" style="behavior:url(webservice.htc)"></div>
</body>
这里我们将div id命名为有意义的名称,并且指定style为 webservice行为。接着,我们要书写javascript来调用webserice了:
首先,我们在javascript中,调用其wsdladdservice.useService("http://localhost/services/math.asmx?WSDL","MyMath");使用id.useService(WSDLL路径,简单的命名方式);
我们之前设定的id是addservice,而为了给客户端调用方便,我们这里起了名称,叫MyMath。而为了保证能正确调用webserice,必须在body里的onload事件里,马上加载处理webservice调用的javascript,如下
<script language="JavaScript">
function init()
{
addservice.useService("http://localhost/services/math.asmx?WSDL","MyMath"); }
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>
在上面,我们通过webservice行为,首先得到了返回webservice的wsdl,接下来我们要进行调用了,调用的格式如下: iCallID = id.FriendlyName.callService([CallbackHandler,] "MethodName", Param1, Param2, ...);
这里id是我们在div里设置的id,而FridndbyName是我们刚才为方面而起的命,这里就是MyMath了,而CallbackHandler是使用回调函数的过程名,如果无设置的话,则默认是使用onresult所调用的方法来进行处理,下文会讲到,而param1,,param2等则是说传入的参数了,如:
<html xmlns="http://www.w3.org/1999/xhtml">2
<head>3
<title>Untitled Page</title>4

5
<script language="JavaScript">6
// All these variables must be global,7
// because they are used in both init() and onresult().8
var iCallID = 0;9
var intA = 5;10
var intB = 6;11
function init()12
{13
// Establish the friendly name "MyMath" for the WebServiceURL14
service.useService("/services/math.asmx?WSDL","MyMath");15
// The following method doesn't specify a callback handler, so onWSresult() is used16
iCallID = service.MyMath.callService("add", intA, intB);17
}18
function onWSresult()19
{20
// if there is an error, and the call came from the call() in init()21
if((event.result.error)&&(iCallID==event.result.id))22
{23
// Pull the error information from the event.result.errorDetail properties24
var xfaultcode = event.result.errorDetail.code;25
var xfaultstring = event.result.errorDetail.string;26
var xfaultsoap = event.result.errorDetail.raw;27
// Add code to handle specific error codes here28
}29
// if there was no error, and the call came from the call() in init()30
else if((!event.result.error) && (iCallID == event.result.id))31
{32
// Show the arithmetic!33
alert(intA + ' + ' + intB + ' = ' + event.result.value);34
}35
else36
{37
alert("Something else fired the event!");38
}39
}40
</script>41
</head>42
<body onload="init()">43
<div id="service" style="behavior: url(webservice.htc)" onresult="onWSresult()">44
</div>45
</body>46
</html>
注意,用onresult方式返回的话,要在div部分的onresult中指定处理的方法,这里是用onWsresult()方法,其中根据返回的信息来判断是否出错,出错的话则显示。
如果用回调的话,则如下处理
<html xmlns="http://www.w3.org/1999/xhtml">2
<head>3
<title>Untitled Page</title>4
<script language="JavaScript">5
//All these variables must be global,6
// because they are used in both init() and onResult().7
var iCallID = 0;8
var intA = 5;9
var intB = 6;10
function init()11
{12
// Establish the friendly name "MyMath" for the WebServiceURL13
service.useService("/services/math.asmx?WSDL","MyMath");14
// The following uses a callback handler named "mathResults"15
iCallID = service.MyMath.callService(mathResults, "add", intA, intB);16
}17
function mathResults(result)18
{19
// if there is an error, and the call came from the call() in init()20
if(result.error)21
{22
// Pull the error information from the event.result.errorDetail properties23
var xfaultcode = result.errorDetail.code;24
var xfaultstring = result.errorDetail.string;25
var xfaultsoap = result.errorDetail.raw;26
// Add code to handle specific error codes here27
}28
// if there was no error29
else30
{31
// Show the arithmetic32
alert(intA + ' + ' + intB + " = " + result.value);33
}34
}35
</script>36
</head>37
<body onload="init()">38
<div id="service" style="behavior: url(webservice.htc)">39
</div>40
</body>41
</html>



浙公网安备 33010602011771号