用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技术技巧大全(五)
Jan 22 Javascript
JavaScript库 开发规则
Jan 31 Javascript
jquery ready()的几种实现方法小结
Jun 18 Javascript
Jquery submit()无法提交问题
Apr 21 Javascript
Javascript中的arguments与重载介绍
Mar 15 Javascript
JS针对Array的各种操作汇总
Nov 29 Javascript
一句jQuery代码实现返回顶部效果(简单实用)
Dec 28 Javascript
bootstrap table实现双击可编辑、添加、删除行功能
Sep 27 Javascript
使用Vue-Awesome-Swiper实现旋转叠加轮播效果&amp;平移轮播效果
Aug 16 Javascript
Node如何后台数据库使用增删改查功能
Nov 21 Javascript
微信小程序实现按字母排列选择城市功能
Nov 25 Javascript
JavaScript中的this/call/apply/bind的使用及区别
Mar 06 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
CI框架中site_url()和base_url()的区别
2015/01/07 PHP
jQuery中:reset选择器用法实例
2015/01/04 Javascript
jquery实现图片随机排列的方法
2015/05/04 Javascript
JavaScript中的toDateString()方法使用详解
2015/06/12 Javascript
jquery实现从数组移除指定的值
2015/06/24 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
2015/11/03 Javascript
jQuery插件imgPreviewQs实现上传图片预览
2016/01/15 Javascript
逻辑表达式中与或非的用法详解
2016/06/06 Javascript
JavaScript类型系统之布尔Boolean类型详解
2016/06/26 Javascript
JavaScript实现时间倒计时跳转(推荐)
2016/06/28 Javascript
微信小程序 for 循环详解
2016/10/09 Javascript
jQuery如何防止Ajax重复提交
2016/10/14 Javascript
AngularJS读取JSON及XML文件的方法示例
2017/05/25 Javascript
JavaScript之Date_动力节点Java学院整理
2017/06/28 Javascript
探讨Vue.js的组件和模板
2017/10/27 Javascript
Vue数据绑定简析小结
2019/05/07 Javascript
taro小程序添加骨架屏的实现代码
2019/11/15 Javascript
Vue检测屏幕变化来改变不同的charts样式实例
2020/10/26 Javascript
[01:25:09]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS DT第二场
2014/05/24 DOTA
[03:27]《辉夜杯》线下训练营 导师CU和海涛指点迷津
2015/10/23 DOTA
python之yield表达式学习
2014/09/02 Python
python实现简单遗传算法
2018/03/19 Python
详解pandas数据合并与重塑(pd.concat篇)
2019/07/09 Python
Pycharm安装Qt Design快捷工具的详细教程
2020/11/18 Python
实列教程 一款基于jquery和css3的响应式二级导航菜单
2014/11/13 HTML / CSS
华为菲律宾官方网站:HUAWEI Philippines
2021/02/23 全球购物
几个Linux面试题笔试题
2016/08/01 面试题
倡议书范文格式
2014/05/12 职场文书
贷款委托书
2014/08/01 职场文书
解除劳动关系协议书范文
2014/09/11 职场文书
交通事故委托书范本
2014/09/28 职场文书
群众路线四风自我剖析材料
2014/10/08 职场文书
创业计划书之溜冰场
2019/10/25 职场文书
如何用python识别滑块验证码中的缺口
2021/04/01 Python
SpringBoot整合Redis入门之缓存数据的方法
2021/11/17 Redis
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL