当前位置: 首页 >> 程序设计 >> .net下用Javascript调用webservice
 

.net下用Javascript调用webservice

作者:      来源:zz     发表时间:2007-02-07     浏览次数:      字号:    

.net 下用javascript调用webservice的话,要用到webservice behavior。下面以一个例子讲解之,比较简单

1 、首先,要创建一个webservice,比如

<%@ webservice language="c#" class=mymath %>
using system;
using system.web.services;
public class mymath {
[webmethod]
public int add(int a, int b)
{
return a + b;
}
[webmethod]
public int subtract(int a, int b)
{
return a - b;
}
}
然后发布,先得到其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等则是说传入的参数了,如:
<script language="javascript">
// all these variables must be global,
// because they are used in both init() and onresult().
var icallid = 0;
var inta = 5;
var intb = 6;
function init()
{
// establish the friendly name "mymath" for the webserviceurl
service.useservice("/services/math.asmx?wsdl","mymath");
// the following method doesnt specify a callback handler, so onwsresult() is used
icallid = service.mymath.callservice("add", inta, intb);
}
function onwsresult()
{
// if there is an error, and the call came from the call() in init()
if((event.result.error)&&(icallid==event.result.id))
{
// pull the error information from the event.result.errordetail properties
var xfaultcode   = event.result.errordetail.code;
var xfaultstring = event.result.errordetail.string;
var xfaultsoap   = event.result.errordetail.raw;
// add code to handle specific error codes here
}
// if there was no error, and the call came from the call() in init()
else if((!event.result.error) && (icallid == event.result.id))
{
// show the arithmetic!
alert(inta + + + intb + = + event.result.value);
}
else
{
alert("something else fired the event!");
}
}
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)" onresult="onwsresult()">
</div>
</body>
 
注意,用onresult方式返回的话,要在div部分的onresult中指定处理的方法,这里是用onwsresult()方法,其中根据返回的信息来判断是否出错,出错的话则显示。   
  如果用回调的话,则如下处理
<script language="javascript">
// all these variables must be global,
// because they are used in both init() and onresult().
var icallid = 0;
var inta = 5;
var intb = 6;
function init()
{
// establish the friendly name "mymath" for the webserviceurl
service.useservice("/services/math.asmx?wsdl","mymath");
// the following uses a callback handler named "mathresults"
icallid = service.mymath.callservice(mathresults, "add", inta, intb);
}
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;
// add code to handle specific error codes here
}
// if there was no error
else
{
// show the arithmetic
alert(inta + + + intb + " = " + result.value);
}
}
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •