Skip Navigation Links
技术文档
·网站建设
·软件使用
·图形设计
·程序开发
·网络应用
·电脑技巧
 
公司介绍
·公司简介
·索仕SRCOS网络应用平台
·索仕网站管理系统
·影视广告制作
·联系我们
 
 

如何用JavaScript调用Web服务——callService/useService

5/14/2009 5:56:21 PM

Web服务在分布式架构中起着重要的角色,在学习Web服务中,对Web Service的一些调用服务的方法做了一些整理。今天主要讲通过JavaScript中的两个方法——useService和callService来调用一个已存在的Web服务。

首先,看一下callService这个方法的语法:

iCallID = sElementID.sFriendlyName.callService([oCallHandler], funcOrObj, oParam);

iCallID是调用服务后返回的ID。

sElementID是useService方法的一个控件元素ID。稍后讲如何用userServie。

sFriendlyName是服务名,比如.NET中Default.asmx,则这里是Default。

oCallHandler是处理响应结果的回调函数,因为有些请求无需关注响应结果,在这里是可选参数。

funcOrObj是web服务中的方法,在.NET中便是标有[WebMethod]的一些公用方法。

oParam是Web Method中的参数,可以是0,1,2,…个参数。

以下是做的一个例子:

        //请求登陆
        function loginRequest() {
            
//服务Default.asmx, 方法CheckLoginByIO
            iCallID = service.Default.callService(loginResponse, "CheckLoginByIO", userid.value, userpwd.value, "127.0.0.1");
        }

        
//响应登陆
        function loginResponse(res) {
            
//调用服务出错
            if (res.error) {
                loginError.innerText 
= res.errorDetail.string;
            }
            
else if (res.value.IsError) {//服务后来业务出错
                loginError.innerText = res.value.ErrorMessage;
            }
            
else if (res.value.IsLogin) {//登陆成功
                loginError.innerText = "login successfully,and your name is " + res.value.UserName;
            }
            
else {//登陆失败
                loginError.innerText = "login failed, username or password is incorrect.";
            }
        }

注意,如果你想知道res.value里有哪些参数,而又不知道业务逻辑那边到底返回了什么参数给你,你直接用Visual Studio 2008可以调试,不要忘了,JavaScript在IDE里的断点调试功能也是无比强大的,不逊于C#.

鉴于对useService的用法,下面顺便讲一下createCallOptions:

        //===================================另一种风格请求Web服务================================//
        //请求执行SQL语句方法
        function executeSQLRequest() {
            
var co = serviceZivsoft.createCallOptions(); //createCallOptions
            co.funcName = "ExecuteSql";//web服务方法名
            iCallID = serviceZivsoft.Default.callService(executeSQLResponse,co,sql.value);
        }

        
//响应执行SQL语句方法
        function executeSQLResponse(res) {
            
if (res.error) {
                Span1.innerText 
= res.errorDetail.string;
            }
            
else {
                
//返回数据库表记录影响条数
                Span1.innerText = res.value;
            }
        }

 

其次,看一下useService如何使用。

useService刚开始让我费解的是哪里来的这个方法,后来发现我们需要去微软官方上下载一个叫webservice.htc的文件。

下载完这个文件,将其放到根目录下,在你的html里写上这样一段代码就轻松搞定:

<body onload="init()" id="serviceZivsoft" style="behavior: url(webservice.htc)"/>

 

其实,这是我个人风格,我喜欢在onload时初始化web服务,初始化代码如下:

        var iCallID;
        
//=====================================
        //       初始化对Web服务的调用
        //      Autor: Lihua
        //      Url: http://www.zivsoft.com
        //=====================================
        function init() {
            
//由于我的Web服务在同一个项目,所以用了相对目录
            serviceZivsoft.useService("Default.asmx?WSDL""Default");
        }

 

关于useService更详细的解释,可以去MSDN上查阅,用法还是比较简单的。

 

最后,给一个完整的HTML如下:

<html>
<head>
    
<title>采用userSErvice调用.NET Web Services</title>

    
<script language="JavaScript" type="text/javascript">
        
var iCallID;
        
//=====================================
        //       初始化对Web服务的调用
        //      Autor: Lihua
        //      Url: http://www.zivsoft.com
        //=====================================
        function init() {
            
//由于我的Web服务在同一个项目,所以用了相对目录
            serviceZivsoft.useService("Default.asmx?WSDL""Default");
        }

        
function Add() {
            
//iCallID = sElementID.sFriendlyName.callService([oCallHandler], funcOrObj, oParam);


            
//iCallID: is the returned ID of the service call. 
            //In case of asynchronous call, this ID should be matched with the ID returned as a property of the result object. 
            //Only when matched can the result object be associated with this service call.
            iCallID = serviceZivsoft.Default.callService(mathResults, "Add", a.value, b.value);
        }

        
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
                lblError.innerHTML = "ERROR. Method call failed!"
                
+ "<br/>iCallID:" + iCallID
                
+ "<br/>Fault Code: " + xfaultcode
                
+ "<br/>Fault String:" + xfaultstring
                
+ "<br/>SOAP Data:" + xfaultsoap
                
+ "<br/>Result:" + result.value;
            }
            
// if there was no error
            else {
                
// Show the arithmetic
                c.value = result.value;
            }
        }

        
//请求登陆
        //--------------ZIVSOFT.COM---------------
        //--------------Lihua Zhou----------------
        function loginRequest() {
            
//服务Default.asmx, 方法CheckLoginByIO
            iCallID = serviceZivsoft.Default.callService(loginResponse, "CheckLoginByIO", userid.value, userpwd.value, "127.0.0.1");
        }

        
//响应登陆
        //--------------ZIVSOFT.COM---------------
        //--------------Lihua Zhou----------------
        function loginResponse(res) {
            
//调用服务出错
            if (res.error) {
                loginError.innerText 
= res.errorDetail.string;
            }
            
else if (res.value.IsError) {//服务后来业务出错
                loginError.innerText = res.value.ErrorMessage;
            }
            
else if (res.value.IsLogin) {//登陆成功
                loginError.innerText = "login successfully,and your name is " + res.value.UserName;
            }
            
else {//登陆失败
                loginError.innerText = "login failed, username or password is incorrect.";
            }
        }

    
</script>

</head>
<body onload="init()" id="serviceZivsoft" style="behavior: url(webservice.htc)">
    
<input id="a" value="2" />+
    
<input id="b" value="3" />=
    
<input id="c" />
    
<input type="button" value="compute" onclick="Add();" />
    
<p>
        
<span id="lblError"></span>
    
</p>
    
<hr />
    
<input id="userid" />
    
<input id="userpwd" type="password" />
    
<input type="button" value="login" onclick="loginRequest();" />
    
<p>
        
<span id="loginError"></span>
    
</p>
</body>
</html>

 

作者:周利华 来源:博客园
 
 
 
昆明索仕科技开发有限公司 版权所有 Copyright© 2002-2010 Kunming Source Technology Exploitive Co.,LTD. All Rights Reserved.
电话:0871-5627877 业务QQ:163871 联系我们
本站基于:索仕网站信息管理系统建设 版本 2.0.4325