简体   繁体   English

使用JavaScript调用Web服务

[英]Web-Service invoke with JavaScript

I am developing my first Web-Service at the moment. 目前,我正在开发我的第一个Web服务。

Client is developed with JavaScript. 客户端是使用JavaScript开发的。

My problem is that it did not work. 我的问题是它没有用。 I do not know what my problem is. 我不知道我的问题是什么。

I think it is a mistake on the client site. 我认为这是客户端网站上的错误。 I tried it with an Java Web-Service Client and there it works. 我使用Java Web服务客户端进行了尝试,并且可以正常工作。

Web-Service: 网络服务:

import javax.jws.*;
import javax.jws.soap.SOAPBinding;
@WebService(name="TicketWebService", targetNamespace = "http://my.org/ns/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class TicketWebService {

  @WebMethod(operationName="getContact")
  public String getContact()
  {

     return "Hallo Hans!!!";
  }
}

Publish on Server: 在服务器上发布:

import javax.swing.JOptionPane;
import javax.xml.ws.Endpoint;

public class PublishWsOnServer
{
  public static void main( String[] args )
  {
    Endpoint endpoint = Endpoint.publish( "http://localhost:8080/services",
                                          new TicketWebService() );
    JOptionPane.showMessageDialog( null, "Server beenden" );
    endpoint.stop(); 
  }
}

Client: 客户:

 <html>
  <head>
   <title>Client</title>
    <script language="JavaScript">
function HelloTo()
{
    var endpoint = "http://localhost:8080/services";
    var soapaction = "http://localhost:8080/services/getContact";

    xmlHttp = getXMLHttp();
    xmlHttp.open('POST', endpoint, true);
    xmlHttp.setRequestHeader('Content-Type', 'text/xml;charset=utf-8');
    xmlHttp.setRequestHeader('SOAPAction', soapaction);

    xmlHttp.onreadystatechange = function() {

       alert(xmlHttp.responseXML);

    }

    xmlHttp.send(request);
}
</script>
    </head>
    <body onLoad="HelloTo()" id="service">
    Body in Client
  </body>
 </html>

The alert does not work... 警报不起作用...

I'm pretty new at JAX-WS but I think that maybe your problem is not in the client side. 我在JAX-WS上还很新,但是我认为也许您的问题不在客户端。 First of all, here you have a HelloWorld example that works fine, if you look into the code you will see that in the web service implementation the annotation WebService is defined as 首先, 这里有一个很好的HelloWorld示例,如果您仔细研究代码,将会发现在Web服务实现中,注释WebService被定义为

@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")

which is the full package of your "TicketWebService". 这是“ TicketWebService”的完整软件包。 Another difference is that the example defines an interface (marked with the @WebService annotation) and then implements it, including the @WebService also in the implementation. 另一个区别是该示例定义了一个接口(标有@WebService批注),然后实现了该接口,在实现中也包括了@WebService。 I don't think this is mandatory, but is a good practice to define the interface. 我不认为这是强制性的,但是定义接口是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM