用JavaScript调用WebService的示例


Posted in Javascript onApril 07, 2008

步骤如下:

下载微软HTC组件:http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/behaviors/library/webservice/default.asp

Web服务WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="WS_SYSTEM_LOGIN_SELECT" targetNamespace="SCRM4U_SERVICE" xmlns:tns="SCRM4U_SERVICE" xmlns:wsdlns="SCRM4U_SERVICE" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns1="SCRM4U_SERVICE">
    <document>
        <URL>http://192.168.2.148:8080/SCRM4U_SERVICE/servlet/HLP_PWS_SYSTEM_LOGIN_SELECT.htm</URL>
        <line>?</line>
    </document>
    <types>
        <schema targetNamespace="SCRM4U_SERVICE" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" elementFormDefault="qualified">
            <complexType name="W_S_LOGIN_USER_INFO">
                <all>
                    <element name="W_V_USID" type="xsd:string">
                    </element>
                    <element name="W_V_USER_MEI" type="xsd:string">
                    </element>
                    <element name="W_V_USID2" type="xsd:string">
                    </element>
                    <element name="W_V_REM_PHR" type="xsd:string">
                    </element>
                    <element name="W_V_REM_RES" type="xsd:string">
                    </element>
                    <element name="W_V_USR_AST" type="xsd:string">
                    </element>
                    <element name="W_V_PSN_NO" type="xsd:string">
                    </element>
                    <element name="W_V_MENUROLE_ID" type="xsd:string">
                    </element>
                    <element name="W_V_USR_AST_NM" type="xsd:string">
                    </element>
                    <element name="W_N_USR_AST_PRI_ORD" type="xsd:long">
                    </element>
                    <element name="W_V_SCHOOL_ID" type="xsd:string">
                    </element>
                    <element name="W_V_EXM_NO" type="xsd:string">
                    </element>
                    <element name="W_V_SCRG_NO" type="xsd:string">
                    </element>
                    <element name="W_V_GRNTR_PSN_NO" type="xsd:string">
                    </element>
                </all>
            </complexType>
            <element name="WS_SYSTEM_LOGIN_SELECT.Execute">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="P_v_userid" type="xsd:string"/>
                        <element minOccurs="1" maxOccurs="1" name="P_v_password" type="xsd:string"/>
                    </sequence>
                </complexType>
            </element>
            <element name="WS_SYSTEM_LOGIN_SELECT.ExecuteResponse">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="P_s_login_user_info" type="tns1:W_S_LOGIN_USER_INFO"/>
                    </sequence>
                </complexType>
            </element>
        </schema>
    </types>
    <message name="WS_SYSTEM_LOGIN_SELECT.ExecuteSoapIn">
        <part name="parameters" element="tns:WS_SYSTEM_LOGIN_SELECT.Execute"/>
    </message>
    <message name="WS_SYSTEM_LOGIN_SELECT.ExecuteSoapOut">
        <part name="parameters" element="tns:WS_SYSTEM_LOGIN_SELECT.ExecuteResponse"/>
    </message>
    <portType name="WS_SYSTEM_LOGIN_SELECTSoapPort">
        <operation name="Execute">
            <input message="wsdlns:WS_SYSTEM_LOGIN_SELECT.ExecuteSoapIn"/>
            <output message="wsdlns:WS_SYSTEM_LOGIN_SELECT.ExecuteSoapOut"/>
        </operation>
    </portType>
    <binding name="WS_SYSTEM_LOGIN_SELECTSoapBinding" type="wsdlns:WS_SYSTEM_LOGIN_SELECTSoapPort">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Execute">
            <soap:operation soapAction="SCRM4U_SERVICEaction/AWS_SYSTEM_LOGIN_SELECT.Execute"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="WS_SYSTEM_LOGIN_SELECT">
        <port name="WS_SYSTEM_LOGIN_SELECTSoapPort" binding="wsdlns:WS_SYSTEM_LOGIN_SELECTSoapBinding">
            <soap:address location="http://192.168.2.148:8080/SCRM4U_SERVICE/servlet/aws_system_login_select"/>
        </port>
    </service>
</definitions>

演示代码:

<html>
<head>
</head>
<script language="javascript">...
function doService(userid, password) ...{

    //service.useService({webservice location},{webservice class name})
    service.useService("http://192.168.2.148:8080/SCRM4U_SERVICE/servlet/aws_system_login_select?wsdl","WS_SYSTEM_LOGIN_SELECT");
    //service.Service.callService({webmethod},{input parameters needed})
    service.WS_SYSTEM_LOGIN_SELECT.callService(showResult, "Execute", userid, password);
}

function showResult(result)...{

    alert(result.value.W_V_USER_MEI);
}
</script>
<body>
<div id="service" style="behavior:url(./webservice.htc)" ></div>
UserName:<input type="text" id="userid" name="userid">
<br/>
PassWord:<input type="text" id="password" name="password">
<br/>
<input type="button" value="callWebservice" onClick="doService(document.getElementById('userid').value,document.getElementById('password').value)"/>
</body>
</html>
注意:如果服务返回值类型是String,直接用result.value即可取到值。

Javascript 相关文章推荐
JavaScript使用cookie
Feb 02 Javascript
JavaScript 异步方法队列链实现代码分析
Jun 05 Javascript
js实现翻页后保持checkbox选中状态的实现方法
Nov 03 Javascript
jquery实现的下拉和收缩效果示例
Aug 21 Javascript
纯javascript实现图片延时加载方法
Aug 21 Javascript
JS获取本周周一,周末及获取任意时间的周一周末功能示例
Feb 09 Javascript
three.js实现3D视野缩放效果
Nov 16 Javascript
Vue底层实现原理总结
Feb 17 Javascript
记录vue项目中遇到的一点小问题
May 14 Javascript
VUE脚手架具体使用方法
May 20 Javascript
layui 表格操作列按钮动态显示的实现方法
Sep 06 Javascript
使用Vant完成DatetimePicker 日期的选择器操作
Nov 12 Javascript
HTML中Select不用Disabled实现ReadOnly的效果
Apr 07 #Javascript
javascript add event remove event
Apr 07 #Javascript
JS动画效果代码3
Apr 03 #Javascript
CSS常用网站布局实例
Apr 03 #Javascript
地址栏上的一段语句,改变页面的风格。(教程)
Apr 02 #Javascript
IE和Firefox在JavaScript应用中的兼容性探讨
Apr 01 #Javascript
JavaScript的面向对象方法以及差别
Mar 31 #Javascript
You might like
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
[原创]php逐行读取txt文件写入数组的方法
2015/07/02 PHP
PHP explode()函数的几个应用和implode()函数有什么区别
2015/11/05 PHP
PHP实现的一致性Hash算法详解【分布式算法】
2018/03/31 PHP
PHP里的$_GET数组介绍
2019/03/22 PHP
Nigma vs Alliance BO5 第五场2.14
2021/03/10 DOTA
获取当前网页document.url location.href区别总结
2008/05/10 Javascript
javascript获取当前日期时间及其它操作函数
2011/01/11 Javascript
JS中类或对象的定义说明
2014/03/10 Javascript
jQuery学习笔记之jQuery.fn.init()的参数分析
2014/06/09 Javascript
JavaScript实现基于十进制的四舍五入实例
2015/07/17 Javascript
jQuery中delegate()方法的用法详解
2016/10/13 Javascript
完美解决node.js中使用https请求报CERT_UNTRUSTED的问题
2017/01/08 Javascript
JS获得多个同name 的input输入框的值的实现方法
2017/01/09 Javascript
详解angularjs结合pagination插件实现分页功能
2017/02/10 Javascript
详解在AngularJS的controller外部直接获取$scope
2017/06/02 Javascript
zTree树形插件异步加载方法详解
2017/06/14 Javascript
angular将html代码输出为内容的实例
2018/09/30 Javascript
vue.js封装switch开关组件的操作
2020/10/26 Javascript
Java 生成随机字符的示例代码
2021/01/13 Javascript
python 解析XML python模块xml.dom解析xml实例代码
2014/02/07 Python
Python循环语句之break与continue的用法
2015/10/14 Python
Python实现树莓派WiFi断线自动重连的实例代码
2017/03/16 Python
flask实现验证码并验证功能
2019/12/05 Python
python 将html转换为pdf的几种方法
2020/12/29 Python
css3 flex布局 justify-content:space-between 最后一行左对齐
2020/01/02 HTML / CSS
HTML5实现页面切换激活的PageVisibility API使用初探
2016/05/13 HTML / CSS
巴西最好的男鞋:Rafarillo
2018/05/25 全球购物
护理工作感言
2014/01/16 职场文书
开工典礼策划方案
2014/05/23 职场文书
学生顶撞老师的检讨书
2014/09/17 职场文书
党支部考察意见范文
2015/06/02 职场文书
初中班干部工作总结
2015/08/10 职场文书
python 遍历磁盘目录的三种方法
2021/04/02 Python
HTML+CSS 实现顶部导航栏菜单制作
2021/06/03 HTML / CSS
深入浅析Django MTV模式
2021/09/04 Python