使用 JavaScript 调用 Web 服务
我目前正在开发我的第一个网络服务。
客户端是用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是 JAX-WS 的新手,但我认为您的问题可能不在客户端。首先,这里你有一个HelloWorld 示例工作正常,如果您查看代码,您会发现在 Web 服务实现中,注释 WebService 被定义为
“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
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.