JavaScript Calling WebService by SOAP

 
Steps:
 
1. 对于不同浏览器,取得相应XMLHTTP
2. 拼接SOAP message;
3. POST方法;
4. Send;
 
PS: WebService 是Java Axis2搭建的。
  1. <script type="text/javascript">
  2. function getXmlHttp(){
  3. var xmlHttp;
  4. if(window.XMLHttpRequest)
  5. {// code for IE7+, Firefox, Chrome, Opera, Safari
  6. xmlHttp =newXMLHttpRequest();
  7. }
  8. else
  9. {// code for IE6, IE5
  10. xmlHttp =newActiveXObject("Microsoft.XMLHTTP");
  11. }
  12. return xmlHttp;
  13. }
  14. functionRequestWebService(){
  15. //Webservice location
  16. var URL ="http://10.253.148.124:8080/OncallWebServer/services/OncallWebServer?wsdl";
  17. //Generate SOAP request
  18. var ptype = document.getElementById('ptype').value;
  19. var year = document.getElementById('year').value;
  20. var month = document.getElementById('month').value;
  21. var pday = document.getElementById('pday').value;
  22. var pchg = document.getElementById('pchg').value;
  23. var data;
  24. data ='<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:axis="http://ws.apache.org/axis2">';
  25. data = data +'<soap:Header/>';
  26. data = data +'<soap:Body>';
  27. data = data +'<axis:MessagePush>';
  28. data = data +'<axis:type>';
  29. data = data + ptype;
  30. data = data +'</axis:type>';
  31. data = data +'<axis:year>';
  32. data = data + year;
  33. data = data +'</axis:year>';
  34. data = data +'<axis:month>';
  35. data = data + month;
  36. data = data +'</axis:month>';
  37. data = data +'<axis:day>';
  38. data = data + pday;
  39. data = data +'</axis:day>';
  40. data = data +'<axis:change>';
  41. data = data + pchg;
  42. data = data +'</axis:change>';
  43. data = data +'</axis:MessagePush>';
  44. data = data +'</soap:Body>';
  45. data = data +'</soap:Envelope>';
  46. var xmlHttp = getXmlHttp();
  47. xmlHttp.open('POST', URL,true);
  48. //xmlHttp.onreadystatechange=state_Change;
  49. xmlHttp.onreadystatechange =function(){
  50. if(xmlHttp.readyState ==4){
  51. try{
  52. if(xmlHttp.status ==200&&typeof(success)=='function'){
  53. success(xmlHttp.responseText);
  54. }
  55. elseif((xmlHttp.status /100==4|| xmlHttp.status /100==5)&&typeof(error)=='function'){
  56. error(xmlHttp.responseText, xmlHttp.status);
  57. }
  58. elseif(xmlHttp.status /100==200&&typeof(complete)=='function'){
  59. complete(xmlHttp.responseText, xmlHttp.status);
  60. }
  61. elseif(typeof(failed)=='function'){
  62. failed(xmlHttp.responseText, xmlHttp.status);
  63. }
  64. }
  65. catch(e){
  66. }
  67. }
  68. }
  69. xmlHttp.setRequestHeader("Content-Type","application/soap+xml");
  70. xmlHttp.send(data);
  71. //Display reply message - file location (for testing)
  72. document.getElementById("data").innerHTML = xmlHttp.responseText;
  73. }
  74. </script>
 
 
 





posted @ 2015-07-15 13:18  ECNB  阅读(195)  评论(0编辑  收藏  举报