Java 如何从JQuery调用soap web服务
我已经开发了一个用于从JQuery调用soap web服务的示例应用程序,但在查找服务时遇到了一些问题。我使用的是ApacheCXF2.2.6 JAR 我用了这个Java 如何从JQuery调用soap web服务,java,html,web-services,jquery,Java,Html,Web Services,Jquery,我已经开发了一个用于从JQuery调用soap web服务的示例应用程序,但在查找服务时遇到了一些问题。我使用的是ApacheCXF2.2.6 JAR 我用了这个 从JQuery创建webservice cal的步骤 这是我的界面 package guru.service; import javax.jws.WebService; import javax.jws.WebParam; import javax.xml.ws.RequestWrapper; @WebService( targetN
从JQuery创建webservice cal的步骤 这是我的界面
package guru.service;
import javax.jws.WebService;
import javax.jws.WebParam;
import javax.xml.ws.RequestWrapper;
@WebService( targetNamespace = "http://www.local-guru.net/helloService" )
public interface HelloService {
@RequestWrapper( targetNamespace="http://www.local-guru.net/helloService" )
public String hello(@WebParam( name="givenname", targetNamespace="http://www.local-guru.net/helloService" ) String givenname, @WebParam( name="name", targetNamespace="http://www.local-guru.net/helloService" ) String name );
}
我的实现类是
package guru.service;
import javax.jws.WebService;
@WebService( endpointInterface="guru.service.HelloService", serviceName="HelloService", targetNamespace = "http://www.local-guru.net/helloService" )
public class HelloServiceImpl implements HelloService{
public String hello( String givenname, String name ) {
return "Hello " + givenname + " " + name + "!";
}
}
和我的应用程序上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="helloServiceImpl" class="guru.service.HelloServiceImpl" />
<jaxws:endpoint id="helloService"
implementor="#helloServiceImpl" address="/helloService"/>
</beans>
我可以使用此URL查看我的WSDl定义
我使用SOAPUI3.6.1软件为此服务创建了soap请求
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://www.local-guru.net/helloService">
<soapenv:Header/>
<soapenv:Body>
<hel:hello>
<hel:givenname>MAYA</hel:givenname>
<hel:name>MANI</hel:name>
</hel:hello>
</soapenv:Body>
</soapenv:Envelope>
玛雅人
马尼
我的服务回应是
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:helloResponse xmlns:ns2="http://www.local-guru.net/helloService">
<return>Hello MAYA MANI!</return>
</ns2:helloResponse>
</soap:Body>
</soap:Envelope>
你好,玛雅·马尼!
但是当我尝试使用jquery调用我的服务时,我得到了这个错误
警告:拦截器用于
{http://www.local-guru.net/helloService}HelloService已抛出
异常,立即展开org.apache.cxf.binding.soap.SoapFault:错误
阅读XMLStreamReader。在
org.apache.cxf.binding.soap.interceptor.ReadHeaderInterceptor.handleMessage(ReadHeaderInterceptor.java:230)
在
org.apache.cxf.binding.soap.interceptor.ReadHeaderInterceptor.handleMessage(ReadHeaderInterceptor.java:60)
在
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
在
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109)
在
org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:98)
在
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:406)
在
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:178)
在
org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142)
在
org.apache.cxf.transport.servlet.AbstractHTTPServlet.HandlerRequest(AbstractHTTPServlet.java:179)
在
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doOptions(AbstractHTTPServlet.java:132)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
在
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
在
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
在
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
在
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
在
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
在
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
在
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
在
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
在
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
在
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
在
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
在
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
在
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
在java.lang.Thread.run(Thread.java:619)处,由以下原因引起:
com.ctc.wstx.exc.wstxeof异常:在
[行,列{未知源}]:[1,0]位于
StreamScanner.throwunnexpectedeof(StreamScanner.java:686)
在
com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2134)
在
com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2040)
在
com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1069)
在
com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1095)
在
org.apache.cxf.binding.soap.interceptor.ReadHeaderInterceptor.handleMessage(ReadHeaderInterceptor.java:122)
... 25多
我的html页面是
<html>
<head>
<script src="jquery.js"></script>
<script>
function callservice() {
var givenname = $("#givenName").val();
var name = $("#name").val();
var q ='<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://www.local-guru.net/helloService"><soapenv:Header/><soapenv:Body><hel:hello><hel:givenname>'+givenname+'</hel:givenname><hel:name>'+name+'</hel:name></hel:hello></soapenv:Body></soapenv:Envelope>'
$.ajax({
url: 'http://192.168.1.209:8080/sampleWebService/helloService/hello',
data: q,
type: 'post',
contentType: 'text/xml; charset="utf-8"',
success: function(res) {
$('#result').html(
res.getElementsByTagNameNS("http://www.local-guru.net/helloService",
"helloResponse")[0].childNodes[0].childNodes[0].nodeValue);
},
});
}
</script>
</head>
<body>
<form>
<input type="text" id="givenName"/>
<input type="text" id="name"/>
<input type="button" onClick="callservice()"/>
</form>
<div id="result">result</div>
</body>
</html>
函数callservice(){
var givenname=$(“#givenname”).val();
var name=$(“#name”).val();
变量q=''+givenname+''+name+''
$.ajax({
网址:'http://192.168.1.209:8080/sampleWebService/helloService/hello',
数据:q,
键入:“post”,
contentType:'text/xml;charset=“utf-8”',
成功:功能(res){
$('#result').html(
res.getelementsbytagnames(“http://www.local-guru.net/helloService",
“helloResponse”)[0]。childNodes[0]。childNodes[0]。nodeValue);
},
});
}
结果
请帮助我解决此问题。浏览器阻止跨域访问。 尝试在webservice域中部署HTML页面 有关更多信息:
根据您发布的错误,在我看来,您的Ajax请求中的XML负载的格式不正确,或者不是服务器所期望的,您是否在浏览器中检查了请求和响应?在这种情况下(经常出现IME),查看预期的SOAP请求和实际发送的请求之间的差异是非常宝贵的 我建议,如果上述情况属实,那么很可能您对行结尾或xml的“q”字符串的编码有问题。要进行测试,请尝试以下方法:
它从磁盘解析一个“格式良好”的xml文件,然后将其发送:-)嗨,卢卡,我已经这样做了,但仍然是同一个问题。我只将html保存在运行web服务的服务器中。您找到解决方案了吗?