最近在做webservice相关的东西,我是采用SOAP的方式进行调用的,网上的资料都是调用的.net发布的webservice,和java发布的东西有点不一样,就是这点不一样困扰了我好久。我参考的是这位大侠的代码:http://blog.csdn.net/lynnlin1122/archive/2009/02/05/3864713.aspx
贴上我webservice的代码(使用XFire发布):package com.xiaoair.service;public interface IGISWebService {

public String example(String message);

public String showTime();

}实现:package com.xiaoair.service;public class GISWebServiceImpl implements IGISWebService {

public String example(String message) {
return message;
} public String showTime() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = sdf.format(new java.util.Date());
return time;
}
}我在用MyEclipse自带的SOAP工具测试的时候得到的是这两段代码:
SOAP Request Envelope:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://service.xiaoair.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:example>
   <q0:in0>nihao</q0:in0> 
   </q0:example>
   </soapenv:Body>
</soapenv:Envelope>SOAP Response Envelope:<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<exampleResponse xmlns:ns1="http://service.xiaoair.com">
   <ns1:out>nihao</ns1:out> 
   </exampleResponse>
   </soap:Body>
</soap:Envelope>贴上我自己的代码: <script type="text/javascript">
function CallWebService(value){
var data;   
         data = '<?xml version="1.0" encoding="utf-8"?>';    
         data = data + '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://service.xiaoair.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';    
         data = data + '<soap:Body>';
data = data + '<q0:example>'; 
data = data + '<q0:in0>'+value+'</q0:in0>';
data = data + '</q0:example>';
data = data + '</soap:Body>';    
         data = data + '</soap:Envelope>';

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL = 'http://localhost:8080/WebServiceShow/services/GISWebService?wsdl';
xmlhttp.Open("POST",URL, false); 
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://service.xiaoair.com"); 
xmlhttp.Send(data);    
         document.write( xmlhttp.responseText);
}

</script>
  <body>
    <h1 align="center">在页面中使用JavaScript调用webservice--SOAP初步</h1>
    <input type="button" value="JavaScript调用webservice" onClick="CallWebService('nihaonihao')"/>
  </body>
我点击按钮之后,没有反应,查看源文件时发现源文件已经变成了http://localhost:8080/WebServiceShow/services/GISWebService?wsdl了,内容一模一样。忙这个东西忙了好久了头好大好大
总觉得这种办法不怎么好,拼字符串只要有一处拼错了结果就出不来,希望各位大侠能够指点一二,多谢了。

解决方案 »

  1.   

    占用大家一点时间,希望了解这方面知识的大侠指点一二,不胜感激。
    刚刚上网查了一下,貌似用jQuery+ajax来做会方便一点,希望哪位了解这方面知识的大侠指点一二,先谢谢了。
      

  2.   

    1、你用Java代码发送soap的xml代码应该是没有问题的..我做个这个
    2、我还真没有用js发送过..不过我倒是觉得应该是可以成功的
    你alert(xmlhttp.responseText); 看看
      

  3.   

    alert神马都不出来啊,还是原来那个现象,真要命