0 0

js 调用webservice 问题3

如何用js 调用 webservice  我现在做的是 j2ee项目
谁能给提供一个例子,调用webservice 能否提供一个 通用的 js 跟各个js框架无关的
谢谢!!!!!
问题补充:
=====================
是 能否给写个简单的 例子 谢谢
问题补充:
=====================================
我的webservice 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

<service>
<name>SimpleWs</name>
<namespace>wsclient.server.ws.mjp.com</namespace>
<serviceClass>com.mjp.ws.server.ISimpleWs</serviceClass>
<implementationClass>com.mjp.ws.server.SimpleWsImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service></beans>
ws 方法:
public String example(String message);

js 调用 soap 数据
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><sample xmlns="wsclient.server.ws.mjp.com"><message>{aa:123}</message></sample></soap:Body></soap:Envelope>
然后返回 500 错误
<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><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Invalid operation: {wsclient.server.ws.mjp.com}sample</faultstring></soap:Fault></soap:Body></soap:Envelope>
是啥原因? 谢谢
问题补充:
我java 客户程序调用没有问题
主要是 看的 js ajax调用 是不是有问题?
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="urn:DefaultNamespace" xmlns:tns="urn:DefaultNamespace" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://request.ws.mjp.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:ns2="http://response.ws.mjp.com" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="urn:DefaultNamespace">
- <xsd:element name="example">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="exampleResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="qryInfo">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="ns1:TeamQryRequest" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="qryInfoResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns2:ArrayOfTreamQryResponse" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://request.ws.mjp.com">
- <xsd:complexType name="TeamQryRequest">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://response.ws.mjp.com">
- <xsd:complexType name="ArrayOfTreamQryResponse">
- <xsd:sequence>
  <xsd:element maxOccurs="unbounded" minOccurs="0" name="TreamQryResponse" nillable="true" type="ns2:TreamQryResponse" />
  </xsd:sequence>
  </xsd:complexType>
- <xsd:complexType name="TreamQryResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="addDate" nillable="true" type="xsd:string" />
  <xsd:element minOccurs="0" name="id" nillable="true" type="xsd:string" />
  <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="exampleRequest">
  <wsdl:part name="parameters" element="tns:example" />
  </wsdl:message>
- <wsdl:message name="qryInfoResponse">
  <wsdl:part name="parameters" element="tns:qryInfoResponse" />
  </wsdl:message>
- <wsdl:message name="exampleResponse">
  <wsdl:part name="parameters" element="tns:exampleResponse" />
  </wsdl:message>
- <wsdl:message name="qryInfoRequest">
  <wsdl:part name="parameters" element="tns:qryInfo" />
  </wsdl:message>
- <wsdl:portType name="SimpleWsPortType">
- <wsdl:operation name="example">
  <wsdl:input name="exampleRequest" message="tns:exampleRequest" />
  <wsdl:output name="exampleResponse" message="tns:exampleResponse" />
  </wsdl:operation>
- <wsdl:operation name="qryInfo">
  <wsdl:input name="qryInfoRequest" message="tns:qryInfoRequest" />
  <wsdl:output name="qryInfoResponse" message="tns:qryInfoResponse" />
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="SimpleWsHttpBinding" type="tns:SimpleWsPortType">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="example">
  <wsdlsoap:operation soapAction="" />
- <wsdl:input name="exampleRequest">
  <wsdlsoap:body use="literal" />
  </wsdl:input>
- <wsdl:output name="exampleResponse">
  <wsdlsoap:body use="literal" />
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="qryInfo">
  <wsdlsoap:operation soapAction="" />
- <wsdl:input name="qryInfoRequest">
  <wsdlsoap:body use="literal" />
  </wsdl:input>
- <wsdl:output name="qryInfoResponse">
  <wsdlsoap:body use="literal" />
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="SimpleWs">
- <wsdl:port name="SimpleWsHttpPort" binding="tns:SimpleWsHttpBinding">
  <wsdlsoap:address location="http://localhost:8080/wspro/services/SimpleWs" />
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>
我的 js文件
var webservice = function(){
var nameSpace = "DefaultNamespace";
var url = "http://localhost:8080/wspro/services/SimpleWs";
//创建 xmlhttp
var xmlhttp ;
var callback;
var createXmlHttp = function(){
  if(window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequst){
    xmlhttp=new XMLHttpRequest();
  }
};
//回调处理
var setChange = function(){
  if(xmlhttp.readyState==4 ){
alert(xmlhttp.status);
alert(xmlhttp.responseText);
document.getElementById("tt").value = xmlhttp.responseText;
if(xmlhttp.status==200){
     alert(xmlhttp.responseText);
returnData = {};
if(callback){
if(xmlhttp.responseText !=""){
var json = eval("(" + xmlhttp.responseText + ")");
returnData = json;
}
callback(returnData);
}
  }
  }
  
};
//获取字符串长度
var getlen = function(str){
var bytesCount=0;
for (var i = 0; i < str.length; i++){
var c = str.charAt(i);
if (/^[u0000-u00ff]$/.test(c)){
bytesCount += 1;
}else{
bytesCount += 2;
}
}
return bytesCount;
}; 
//获取数据串
var getData = function(method,str){
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data += '<soap:Body>';
data += '<urn:' + method + ' xmlns="' + nameSpace + '">';
   data += '<message>'+ str +'</message>';
data += "</urn:" + method + ">";
data += '</soap:Body>';
data += '</soap:Envelope>';
return data;
};
var setRequest = function(method,data){
createXmlHttp();
xmlhttp.onreadystatechange = setChange;
xmlhttp.open("POST",url, true);
xmlhttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length",getlen(data));
xmlhttp.setRequestHeader("SOAPAction", nameSpace + method);
xmlhttp.send(data);
}
return {
postWs : function(method,data,call){
var data = getData(method,data);
callback = call;
document.getElementById("t").value = data;
setRequest(method,data);
}
}
}();
我的调用方式:
webservice.postWs("sample","adsfas",function(data){
  alert(data);
  });
问题补充:
我的程序源码:
2009年11月04日 22:12

4个答案 按时间排序 按投票排序

0 0

采纳的答案

var WSDLS = {};

var WebService = new Class({

	url : '',
	method : '',
	options: 
	{
		method:'GET',
		data: null,
		update: null,
		onComplete: Class.empty,
		onError:Class.empty,
		evalScripts: false,
		evalResponse: false
	},
	
	initialize: function(url,method,options)
	{
		this.url = url;
		this.method = method;
		this.options = options;

	},
	
	request : function()
	{
		var wsdl = WSDLS[this.url];
		if(!wsdl) 
		{
			var op = {method:'GET',async: false};
			var wsdlAjax = new XHR(op).send(this.url + "?wsdl", null);			
			wsdl = wsdlAjax.transport.responseXML;
			WSDLS[this.url] = wsdl;
		}
		this.setSoap(wsdl);
	},
		
	setSoap : function(wsdl)
	{
		
		var ns = (wsdl.documentElement.attributes["targetNamespace"] + "" == "undefined") ? wsdl.documentElement.attributes.getNamedItem("targetNamespace").nodeValue : wsdl.documentElement.attributes["targetNamespace"].value;
		var sr = 
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<soap:Envelope " +
				"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
				"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
				"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
				"<soap:Body>" +
				"<" + this.method + " xmlns=\"" + ns + "\">" +
					 (this.options.data === null ?"":this.options.data) +
				"</" + this.method + "></soap:Body></soap:Envelope>";
		
		this.options.method = 'post';
		this.options.data = null;
		
		var soapaction = ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + this.method;
		
		var soapAjax = new Ajax(this.url,this.options);
		soapAjax.setHeader("SOAPAction", soapaction);
		soapAjax.setHeader("Content-type", "text/xml; charset=utf-8");
		soapAjax.request(sr);
	}
	
});

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>JavaScript SOAP Client - DEMOS | GURU4.net</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="js/mootools.v1.11.js" type="text/javascript"></script>
	<script src="js/WebService.js" type="text/javascript"></script>

  	<script type="text/javascript">

  	function ajaxRequest()
    {
    	var url = "http://127.0.0.1:8080/wstest/services/test";

        //设置webService传入参数
        //    调用java(xfire) 发布的webService
        //           传入的参数必须与调用方法的参数数量相等,且按传入值的顺序进行匹配
        //

        var para = "<t>bbbbbb</t><tt>aaaaaaaaaaa</tt>";

        var op = {
					data:para,
                    onComplete: showResponse,
                    onFailure:showError,
                    update:'ajaxBack'
                 };

        var service = new WebService(url,"test",op);
        service.request();
        return false;
    }
    function showError(obj)
    {
		//obj 是一个xmlHttpRequest对象
		alert("error");
    }
    function showResponse(requestText,requestXML)
    {
		//requestText 返回的文本
		//requestXML 返回的XML
		alert("ok");
    }
  	</script>
  </head>

  <body>
     <input type="button" value="执行"  onclick="ajaxRequest()" />
     <div id="ajaxBack"></div>
  </body>
</html>

2009年11月06日 14:29
0 0

看看这个吧 我一直都是用这个的

2009年11月06日 14:29
0 0

500 错误一般都是ws服务端的 你先写个java程序调用试试 如果成功了再用js测试

2009年11月06日 12:55
0 0

ws发送的是soap 只要你能够通过http协议发送soap任何语言都可以 和框架首先肯定无关 框架主要是为了编码简单

2009年11月05日 13:06

相关推荐

Global site tag (gtag.js) - Google Analytics