2011-11-01 65 views
0

我正在開發我的第一個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

回答

0

我JAX-WS相當新穎,但我認爲mayb e您的問題不在客戶端。首先,here你有工作正常,一個HelloWorld示例,如果你看看代碼,你會看到,在Web服務實現註釋的WebService定義爲

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

這是完整的包你的「 TicketWebService」。另一個區別是,該示例定義了一個接口(用@WebService註釋標記),然後實現它,其中包括實現中的@WebService。我不認爲這是強制性的,但是定義界面是一個很好的做法。