-
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个答案 按时间排序 按投票排序
-
采纳的答案
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
相关推荐
Js调用webService示例,在前台页面利用javascript调用后台的webservice方法来实现先后数据的交互(demo)
使用javascript调用webservice示例.pdf使用javascript调用webservice示例.pdf
JavaScript调用WebService实例总结
JS调用webService实例,其中有详细使用文档! myEclipse部署上客户端和服务器端即可直接测试! 使用技术: JS,webService,JS调用webService,xfire,数字证书
JS调用WebService源码
C#中js调用WebService实现无刷新
js 调用WebService组件的相关代码,使用xmlHttprequest进行连接
js原生调用webservice接口,在IE-v11上测试通过,示例中存在参数传入失败的bug,已附源码,望指点解决的方法。
使用javascript 中Ajax技术调用WebService,包括JSP和ASP.NET中两种,其中JSP的WebService使用了XFire框架。 ASP.NET中的WebService做了一个方法示例GetProgress,包含3个参数。 JSP中的WebService做了hello方法的...
js 调用webservice 工具
C#javascript调用webservice C#javascript调用webservice C#javascript调用webservice C#javascript调用webservice
js调用webservice接口,并打印返回信息
webseservice 客户端调用,java发布的axis2 webservice,通过 js调用调用webservice,普通ajax实现,里面有详细注释,代码简单明了,我自己做过测试。希望对大家有所帮助。
三个文档打包下载,分别详细讲述了ajax以及JavaScript调用WebService的具体用法,是学习ajax和WebService的良好示例。
通过一个实例的讲解说明JavaScript是如何调用WebService,希望对大家有帮助。
使用Js动态调用WebService 使用Js动态调用WebService
JS调用WEBSERVICE的demo附带js
C# MVC 用JS 调用webService。打开就能运行!
JS调用WebService的一个小实例,内容虽少,只是这一功能的实现。可运行,asp.net.
Javascript调用Webservice的汇集