使用 JavaScript 调用 Web 服务

发布于 2024-12-13 05:50:49 字数 1674 浏览 4 评论 0原文

我目前正在开发我的第一个网络服务。

客户端是用JavaScript开发的。

我的问题是它不起作用。我不知道我的问题是什么。

我认为这是客户端网站上的一个错误。 我用 Java Web 服务客户端尝试过,它可以工作。

Web 服务:

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!!!";
  }
}

在服务器上发布:

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(); 
  }
}

客户端:

 <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>

警报不起作用...

I am developing my first Web-Service at the moment.

Client is developed with 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.

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2024-12-20 05:50:49

我是 JAX-WS 的新手,但我认为您的问题可能不在客户端。首先,这里你有一个HelloWorld 示例工作正常,如果您查看代码,您会发现在 Web 服务实现中,注释 WebService 被定义为

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

“TicketWebService”的完整包。另一个区别是,该示例定义了一个接口(用@WebService注释标记),然后实现它,在实现中也包括@WebService。我不认为这是强制性的,但这是定义接口的一个好习惯。

I'm pretty new at JAX-WS but I think that maybe your problem is not in the client side. 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

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

which is the full package of your "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. I don't think this is mandatory, but is a good practice to define the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文