xml web services 行为使客户端脚本能够调用由 microsoft .net xml web services 或其他支持简单对象访问协议 (soap) 的 web 服务器公开的远程方法。
目的:提供一种简单的方法使用和利用 soap,而不需具有 soap 实现的专业知识。
步骤:
1.下载webservice.htc,许多网上的文章介绍该文件可以在微软的网站上找到,但我按照地址点进去时,只看到网页不存在的提示
2.创建webservice文件假设为mathservice.asmx,其提供add和subtract两个服务函数
3.创建调用的网页文件sample.htm(文件内容在下面详细分析)
4.将webservice.htc复制到sample.htm同一目录下
5.通过浏览器,浏览该网页
下面分析sample.htm的内容(取自msdn)
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<link rel="stylesheet" href="/workshop/samples/samples.css" type="text/css">
<script language="javascript">
var icallid;
var callobj;
function init()
{
// 定位webservice服务位置,并且为该服务制定一个名字 // 同一serviceurl可以指定多个名字,service为htm文件中的html tag,可以使用html中定义的任何标记
service.useservice("..\\..\\mathservice.asmx?wsdl","mymath");
// 禁用add按钮.
doaddbutton.disabled = true;
service.onserviceavailable = enablebutton();
}
function enablebutton(){
doaddbutton.disabled = false;
}
function doadd(x, y){
// 同步调用 // 创建一个 the soapheader object
var headobj = new object();
// 创建 the call object
callobj = service.createcalloptions();
callobj.async = false;
callobj.params = new array();
callobj.params.a = x;
callobj.params.b = y;
callobj.funcname = "add";
callobj.soapheader = new array();
callobj.soapheader[0] = headobj;
ospan.innertext = x + " + " + y + " = ";
// 使用回调函数"mathresults"调用
icallid = service.mymath.callservice(mathresults, callobj);
mathresults(icallid);
}
function dosubtraction(y, x){
// 异步调用,这是系统默认的调用方式(the default)
ospan.innertext = y + " – " + x + " = ";
// 调用subtract
// 使用回调函数"mathresults"调用
icallid = service.mymath.callservice(mathresults, "subtract", y, x);
}
function mathresults(result){
// if there is an error, and the call came from the call() in init()
if(result.error){
// pull the error information from the event.result.errordetail properties
var xfaultcode = result.errordetail.code;
var xfaultstring = result.errordetail.string;
var xfaultsoap = result.errordetail.raw;
ospan.innertext = xfaultcode + " " + xfaultstring + " " + xfaultsoap;
}// if there was no error
else{
// show the arithmetic
ospan.innertext += result.value;
}
}
</script>
</head>
<body onload="init()">
//设置div元素绑定webservice服务,在这里可以添加onresult="onwsresult()",在该事件中处理调用结果
<div id="service" style="behavior:url(../webservice.htc)"></div>
<br><br>
equation : <span id="ospan"></span>
<br><br>
<button id="doaddbutton" onclick="doadd(5, 6);">do add function of 5 and 6</button>
<button onclick="dosubtraction(6, 5);">do subtraction of 6 and 5</button>
</body>
一些总结:
客户端必须包含webservice.htc,将webservice.htc放在服务器可访问的目录下,可以保证ie浏览器浏览时自动下载该文件,该过程对客户来说是透明的,但是使用其他的浏览器可能会无法使用该功能。
获取调用结果有两种方式
使用事件捕获:在html标记中添加 onresult="onwsresult()",如:
<script language="javascript">
var icallid;
function init()
{
service.useservice("/services/math.asmx?wsdl","mymath");
icallid = service.mymath.callservice("add",5,6);
}
function onwsresult()
{
if((event.result.error)&&(icallid==event.result.id))
{
var xfaultcode = event.result.errordetail.code;
var xfaultstring = event.result.errordetail.string;
var xfaultsoap = event.result.errordetail.raw;
document.writeln("error. method call failed!");
document.writeln("call id:" + icallid);
document.writeln("fault code:" + xfaultcode);
document.writeln("fault string:" + xfaultstring);
document.writeln("soap data:" + xfaultsoap);
}
else if(event.result.error == false)
{
document.writeln("result received without errors!");
}
}
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)" onresult="onwsresult()">
</div>
</body>
使用回调函数:(例子见上面)
调用分为同步调用和异步调用,异步调用为默认的调用方式:(例子见上面)
可以使用callobj进行复杂的调用,设置调用时的参数,以及认证信息
支持有限的数据类型,请参看http://msdn.microsoft.com/workshop/author/webservice/datatypes.asp
