[webservice] 07_客户端调用webservice1——ajax方式

Android 4.0

客户端调用webservice的几种方式

1、通过wsimport方式,最简单的,前面已经做过
2、通过ajax方式调用
通过eclipse中自带的工具生成符合soap协议请求的内容
<html>
    <head>
        <title>通过ajax的方式调用webservice服务</title>
        <script>
            var xhr = new ActiveXObject("Microsoft.XMLHTTP");
            function sendAjaxRequest(){
                //声明webservice服务的地址
                var wsUrl = "http://192.168.1.100:6789/hello";
                //构造请求体,符合SOAP规范
                
                var requestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://publish.webservice.zengfansheng.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:sayHello><arg0>'+document.getElementById("name").value+'</arg0></q0:sayHello></soapenv:Body>'
                
                //打开连接
                xhr.open("POST",wsUrl,true);
                //重新设置请求头
                xhr.setRequestHeader("content-type","text/xml;charset=UTF-8");
                //设置回调函数
                xhr.onreadystatechange = _back;
                //发送请求
                xhr.send(requestBody);
                //alert('发送成功');
            }
            //定义回调函数
            function _back(){
                alert(xhr.readyState);
                if(xhr.readyState == 4){
                    alert(xhr.status);
                    if(xhr.status == 200){
                        //获取服务端返回的数据
                        var ret = xhr.responseXML;
                        var ele = ret.getElementsByTagName('return')[0];
                        //alert(ele.text);
                        document.getElementById('showInfo').innerHTML = ele.text;
                    }
                }
            }
        </script>
    </head>
    <body>
        <input type="text" id="name">
        <input type="button" value="发送ajax请求" onclick="sendAjaxRequest();">
        <div id="showInfo"></div>
    </body>
</html>
使用IE6打开
-    soap请求:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://publish.webservice.zengfansheng.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <q0:sayHello>
  <arg0>hacket</arg0>
  </q0:sayHello>
  </soapenv:Body>