2011-11-02 101 views
1

我嘗試從瀏覽器訪問我開發的簡單郵件列表服務。我修改了一個我發現的工作示例,但我不知道爲什麼請求似乎無法到達該服務,因此沒有響應返回。用SOAP信封調用Web服務使用Javascript失敗

使用SOAP信封是一個需求,我需要知道這段代碼可能有什麼問題,而不是如何使用其他技術做同樣的事情。 (該服務在GlassFish服務器上正確部署,並且我有一個正在工作的Java客戶端來測試它,所以服務沒有問題)

有人看到壞東西(我也加入了WSDL,如果你需要的話其他細節不要猶豫)?謝謝!

<html> 
<head> 
    <title>Soap Invocation</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

    <script type="text/javascript"> 

     var request = null; 

     function createRequest() { 
      if (window.XMLHttpRequest){ 
       request=new XMLHttpRequest(); 
      } 
      else{ 
       if (new ActiveXObject("Microsoft.XMLHTTP")) { 
        request = new ActiveXObject("Microsoft.XMLHTTP"); 
       } else { 
        request = new ActiveXObject("Msxml2.XMLHTTP"); 
       } 
      } 
     } 

     function getMail() { 
      createRequest(); 
      var envelope = "<?xml version='1.0' encoding='UTF-8'?>"; 
      envelope += "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"; 
      envelope += "<soap:Header/>"; 
      envelope += "<soap:Body>"; 
      envelope += "<ns2:getMails xmlns:ns2='http://service.inf4375.com/'>"; 
      envelope += "</ns2:getMails>"; 
      envelope += "</soap:Body>"; 
      envelope += "</soap:Envelope>"; 

      var url = "http://127.0.0.1:8080/SoapService/MailingService"; 

      request.onreadystatechange = updatePage; 

      request.open("GET", url, false); 
      request.setRequestHeader("Content-Type", "text/html"); 
      request.setRequestHeader("SOAPAction", ""); 
      request.send(envelope); 
     } 

     function updatePage() { 

      if(request.readyState == 4) { 
       document.getElementById("get").innerHTML = "<p>" + request.responseXML.selectSingleNode("//return").text + "</p>"; 
      } else { 
       document.getElementById("get").innerHTML = "Loading..." 
      } 

     } 
    </script> 
</head> 
<body> 
    <input type="button" value="GetMail" onclick="getMail();" /> 
    <span id="get"></span>  
</body> 
</html> 

的WSDL:

<?xml version='1.0' encoding='UTF-8'?> 
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.inf4375.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.inf4375.com/" name="MailingService"> 
<types> 
    <xsd:schema> 
     <xsd:import namespace="http://service.inf4375.com/" schemaLocation="http://localhost:8080/SoapService/MailingService?xsd=1"/> 
    </xsd:schema> 
</types> 
<message name="getMails"> 
    <part name="parameters" element="tns:getMails"/> 
</message> 
<message name="getMailsResponse"> 
    <part name="parameters" element="tns:getMailsResponse"/> 
</message> 
<message name="addMail"> 
    <part name="parameters" element="tns:addMail"/> 
</message> 
<message name="addMailResponse"> 
    <part name="parameters" element="tns:addMailResponse"/> 
</message> 
<portType name="Mailing"> 
    <operation name="getMails"> 
     <input wsam:Action="http://service.inf4375.com/Mailing/getMailsRequest" message="tns:getMails"/> 
     <output wsam:Action="http://service.inf4375.com/Mailing/getMailsResponse" message="tns:getMailsResponse"/> 
    </operation> 
    <operation name="addMail"> 
     <input wsam:Action="http://service.inf4375.com/Mailing/addMailRequest" message="tns:addMail"/> 
     <output wsam:Action="http://service.inf4375.com/Mailing/addMailResponse" message="tns:addMailResponse"/> 
    </operation> 
</portType> 
<binding name="MailingPortBinding" type="tns:Mailing"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
    <operation name="getMails"> 
     <soap:operation soapAction=""/> 
     <input> 
      <soap:body use="literal"/> 
     </input> 
     <output> 
      <soap:body use="literal"/> 
     </output> 
    </operation> 
    <operation name="addMail"> 
     <soap:operation soapAction=""/> 
     <input> 
      <soap:body use="literal"/> 
     </input> 
     <output> 
      <soap:body use="literal"/> 
     </output> 
    </operation> 
</binding> 
<service name="MailingService"> 
    <port name="MailingPort" binding="tns:MailingPortBinding"> 
     <soap:address location="http://localhost:8080/SoapService/MailingService"/> 
    </port> 
</service> 

我不明白的錯誤控制檯,當我試圖執行它在Firefox上的任何錯誤,並且Internet Explorer只顯示Loading...等等。

+0

Pure Javascript w/no frameworks? AJAX是一種痛苦... – hugomg

+0

是純粹的Javascript。我發現了一個效果很好的例子,唯一的區別是它發出'POST'請求。 – talnicolas

回答

3

SOAP必須通過POST發送,而不是GET。

此外,它看起來像您的WS-Messaging標頭是錯誤的。事實上,我甚至都沒有看到它們。

嘗試使用.NET客戶端發出此調用(「添加服務引用」),然後用Fiddler或類似的東西觀察線路,以查看發生了什麼。然後做同樣的事情。

+0

非常感謝您,我只是將'GET'換成'POST',現在工作得很好。 – talnicolas