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

来源:互联网 发布:山西农大软件工程学院 编辑:程序博客网 时间:2024/05/04 11:34

 

 

  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:

        //请求执行SQL语句方法
        function executeSQLRequest() {
            var co = service.createCallOptions(); //createCallOptions
            co.funcName = "ExecuteSql";//web服务方法名
            iCallID = service.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 style="behavior: url(webservice.htc)"/>

  在onload时初始化web服务,初始化代码如下:

        var iCallID;
        function init() {
            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;
        function init() {
            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;
            }
        }

        //请求登陆
        function loginRequest() {
            //服务Default.asmx, 方法CheckLoginByIO
            iCallID = serviceZivsoft.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.";
            }
        }

    </script>

</head>
<body style="behavior: url(webservice.htc)">
    <input value="2" />+
    <input value="3" />=
    <input />
    <input type="button" value="compute" />
    <p>
        <span ></span>
    </p>
    <hr />
    <input />
    <input type="password" />
    <input type="button" value="login" />
    <p>
        <span ></span>
    </p>
</body>
</html>

原创粉丝点击