使用JavaScript调用Web服务

问题描述:

我正在开发我的第一个Web服务。使用JavaScript调用Web服务

客户端是用JavaScript开发的。

我的问题是它没有工作。我不知道我的问题是什么。

我认为这是客户端网站上的错误。 我用Java 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> 

警报不工作...

+0

你知道请求是否到达客户端?在服务中创建一些示例输出,例如的System.out.println。 – home

+0

感谢您的回答... 我在getContact()方法中写入了一个system.out.println() 应该在哪里放置输出? 我发现它没有... – user959456

+0

输出应该可以在启动Java Web Service(main []方法)的控制台中使用。 – home

我JAX-WS相当新颖,但我认为mayb e您的问题不在客户端。首先,here你有工作正常,一个HelloWorld示例,如果你看看代码,你会看到,在Web服务实现注释的WebService定义为

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

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