如何从JQuery调用soapweb服务



我已经开发了一个示例应用程序,用于从JQuery调用soapweb服务,但在查找服务时遇到了一些问题。我使用的是apachecxf2.2.6jars。

我用过这个
http://www.local-guru.net/blog/2010/1/24/calling-webservices-from-mootools-jquery-or-dojo从JQuery 创建Web服务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定义

http://192.168.1.209:8080/sampleWebService/helloService?wsdl

使用soapUI 3.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.soap故障:错误正在读取XMLStreamReader。在org.apache.cxf.binding.soap.intercept.ReadHeadersInterceptor.handleMessage(ReadHeadersIinterceptor.java:230)在org.apache.cxf.binding.soap.intercept.ReadHeadersInterceptor.handleMessage(ReadHeadersIinterceptor.java:60)在org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseIntersectorChain.java:243)在org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109)在org.apache.cxf.transport.servlet.ServletDestination.invoke(servlet目的地.java:98)在org.apache.cxf.transport.servlet.ServletController.invokeDestination(servlet控制器.java:406)在org.apache.cxf.transport.servlet.ServletController.ioke(ServletController.java:178)在org.apache.cxf.transport.servlet.AbstractCXFServlet.ioke(AbstractCXFSservlet.java:142)在org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)在org.apache.cxf.transport.servlet.AbstractHTTPServlet.doOptions(AbstractHTTPServlet.java:132)位于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.StandardWrapperValv.invoke(StandardWrapperValve.java:213)在org.apache.catalina.core.StandardContextValv.invoke(StandardContextValv.java:178)在org.apache.catalina.core.StandardHostValv.invoke(StandardHostValve.java:126)在org.apache.catalina.vals.ErrorReportValve.ioke(ErrorReportValve.java:105)在org.apache.catalina.core.StandardEngineValv.invoke(StandardEngineValv.java:107)在org.apache.catalina.connecter.CoyoteAdapter.service(Coyotedapter.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.LaderFollowerWorkerThread.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.WstxEOFException:位于的prolog中出现意外EOF[行,列{未知源}]:[1,0],位于com.ctc.wstx.sr.StreamScanner.sthrowUnexpectedEOF(StreamScanner.java:686)在com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamLeader.java:2134)在com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamLeader.java:2040)在com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamLeader.java:1069)在com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamLeader.java:1095)在org.apache.cxf.binding.soap.intercept.ReadHeadersInterceptor.handleMessage(ReadHeadersIinterceptor.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>

请帮我解决这个问题。

从您发布的错误来看,Ajax请求中的XML负载格式不正确,或者不是服务器所期望的,您是否在浏览器中检查了请求和响应?在这种情况下(通常会弹出IME),查看预期的SOAP请求和实际发送的请求之间的差异是非常宝贵的。

我建议,如果以上是真的,那么很可能您在行结尾或xml的"q"字符串编码方面有问题。要进行测试,请尝试以下操作:http://www.w3schools.com/dom/dom_loadxmldoc.asp

它从磁盘中解析一个"格式良好"的xml文件,然后发送它:-)

Browser阻止跨域访问。尝试在Web服务域中部署HTML页面。

有关更多信息:http://enable-cors.org/

最新更新