如何从JQuery调用soap Web服务

时间:2012-03-06 15:31:13

标签: java html web-services jquery

我已经开发了一个从JQuery调用soap web服务的示例应用程序,但是我遇到了一些查找服务的问题。我正在使用apache cxf 2.2.6 jar。

  

我用过这个   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软件我为此服务创建了肥皂请求

<?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.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:230)     在   org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.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.handleRequest(AbstractHTTPServlet.java:179)     在   org.apache.cxf.transport.servlet.AbstractHTTPServlet.doOptions(AbstractHTTPServlet.java:132)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:718)at   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.WstxEOFException:prolog中的意外EOF   [row,col {unknown-source}]:[1,0] at   com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(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.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.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>

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

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

我建议如果上述情况属实,很可能你遇到了行结尾或xml“q”字符串编码的问题。要进行测试,请尝试以下操作:http://www.w3schools.com/dom/dom_loadxmldoc.asp

从磁盘解析“格式良好”的xml文件,然后发送它: - )

答案 1 :(得分:0)

浏览器阻止跨域边界访问。 尝试在Web服务域中部署HTML页面。

欲了解更多信息: http://enable-cors.org/