JavaScript Calling WebService by SOAP
Steps:
1. 对于不同浏览器,取得相应XMLHTTP
2. 拼接SOAP message;
3. POST方法;
4. Send;
PS: WebService 是Java Axis2搭建的。
<script type="text/javascript">
function getXmlHttp(){
var xmlHttp;
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp =newXMLHttpRequest();
}
else
{// code for IE6, IE5
xmlHttp =newActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
functionRequestWebService(){
//Webservice location
var URL ="http://10.253.148.124:8080/OncallWebServer/services/OncallWebServer?wsdl";
//Generate SOAP request
var ptype = document.getElementById('ptype').value;
var year = document.getElementById('year').value;
var month = document.getElementById('month').value;
var pday = document.getElementById('pday').value;
var pchg = document.getElementById('pchg').value;
var data;
data ='<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:axis="http://ws.apache.org/axis2">';
data = data +'<soap:Header/>';
data = data +'<soap:Body>';
data = data +'<axis:MessagePush>';
data = data +'<axis:type>';
data = data + ptype;
data = data +'</axis:type>';
data = data +'<axis:year>';
data = data + year;
data = data +'</axis:year>';
data = data +'<axis:month>';
data = data + month;
data = data +'</axis:month>';
data = data +'<axis:day>';
data = data + pday;
data = data +'</axis:day>';
data = data +'<axis:change>';
data = data + pchg;
data = data +'</axis:change>';
data = data +'</axis:MessagePush>';
data = data +'</soap:Body>';
data = data +'</soap:Envelope>';
var xmlHttp = getXmlHttp();
xmlHttp.open('POST', URL,true);
//xmlHttp.onreadystatechange=state_Change;
xmlHttp.onreadystatechange =function(){
if(xmlHttp.readyState ==4){
try{
if(xmlHttp.status ==200&&typeof(success)=='function'){
success(xmlHttp.responseText);
}
elseif((xmlHttp.status /100==4|| xmlHttp.status /100==5)&&typeof(error)=='function'){
error(xmlHttp.responseText, xmlHttp.status);
}
elseif(xmlHttp.status /100==200&&typeof(complete)=='function'){
complete(xmlHttp.responseText, xmlHttp.status);
}
elseif(typeof(failed)=='function'){
failed(xmlHttp.responseText, xmlHttp.status);
}
}
catch(e){
}
}
}
xmlHttp.setRequestHeader("Content-Type","application/soap+xml");
xmlHttp.send(data);
//Display reply message - file location (for testing)
document.getElementById("data").innerHTML = xmlHttp.responseText;
}
</script>