ajax调用webservice服务

ajax调用是 html方向调用的, 而sqlconnection是 java代码调用的,本质差不多

 1 <html>
 2     <head>
 3         <title>通过ajax调用webservice服务</title>
 4         <script>
 5             var xhr;
 6             function sendAjaxWS(){
 7                 xhr = new ActiveXObject("Microsoft.XMLHTTP");
 8                 //指定ws的请求地址
 9                 var wsUrl = "http://192.168.1.108:5678/hello";
10                 //手动构造请求体
11                 var requestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' + 
12                                     ' xmlns:q0="http://service.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema "'+
13                                     ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
14                                     '<soapenv:Body><q0:sayHello><arg0>'+document.getElementById("msg").value+'</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>';
15                 //打开连接
16                 xhr.open("POST",wsUrl,true);
17                 //重新设置请求头
18                 xhr.setRequestHeader("content-type","text/xml;charset=utf8");
19                 //设置回调函数
20                 xhr.onreadystatechange = _back;
21                 //发送请求
22                 xhr.send(requestBody);
23             }
24 
25             //定义回调函数
26             function _back(){
27                 if(xhr.readyState == 4){
28                     if(xhr.status == 200){
29                         var ret = xhr.responseXML;
30                         //解析xml
31                         var eles = ret.getElementsByTagName("return")[0];
32                         alert(eles.text);
33                     }
34                 }
35             }
36         </script>
37     </head>
38     <body>
39         <input type="text" id="msg" />
40         <input type="button" onclick="sendAjaxWS();" value="通过ajax调用webservice服务"/>
41     </body>
42 </html>

 

posted on 2014-06-24 11:02  wf110  阅读(688)  评论(0编辑  收藏  举报