WEB开发网
开发学院网页设计JavaScript 如何用JavaScript调用Web服务——callService/use... 阅读

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

 2010-09-14 13:45:07 来源:WEB开发网   
核心提示: 下载完这个文件,将其放到根目录下,如何用JavaScript调用Web服务——callService/useService(3),在你的html里写上这样一段代码就轻松搞定:<body onload="init()" id="serviceZivsoft

下载完这个文件,将其放到根目录下,在你的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>

出处::http://www.cnblogs.com/architect/archive/2009/05/14/1456818.html

上一页  1 2 3 

Tags:如何 JavaScript 调用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接