ajax调用webservice服务

时间:2024-01-08 09:33:14

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

 <html>
<head>
<title>通过ajax调用webservice服务</title>
<script>
var xhr;
function sendAjaxWS(){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
//指定ws的请求地址
var wsUrl = "http://192.168.1.108:5678/hello";
//手动构造请求体
var requestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
' xmlns:q0="http://service.itcast.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("msg").value+'</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>';
//打开连接
xhr.open("POST",wsUrl,true);
//重新设置请求头
xhr.setRequestHeader("content-type","text/xml;charset=utf8");
//设置回调函数
xhr.onreadystatechange = _back;
//发送请求
xhr.send(requestBody);
} //定义回调函数
function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var ret = xhr.responseXML;
//解析xml
var eles = ret.getElementsByTagName("return")[0];
alert(eles.text);
}
}
}
</script>
</head>
<body>
<input type="text" id="msg" />
<input type="button" onclick="sendAjaxWS();" value="通过ajax调用webservice服务"/>
</body>
</html>