【问题标题】:Calling Web Service using Javascript with a SOAP envelope fails使用带有 SOAP 信封的 Javascript 调用 Web 服务失败
【发布时间】:2011-11-02 02:13:21
【问题描述】:

我尝试从浏览器访问我开发的一个简单的邮件列表服务。我改编了我找到的一个工作示例,但我不知道为什么请求似乎没有到达服务,因此没有返回响应。

使用 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...,仅此而已。

【问题讨论】:

  • 纯 Javascript 没有框架? AJAX 是一种痛苦......
  • 是的纯 Javascript。我找到了一个运行良好的示例,唯一的区别是它发出了POST 请求。

标签: javascript web-services soap


【解决方案1】:

SOAP 必须通过 POST 而不是 GET 发送。

此外,您的 WS-Messaging 标头似乎有误。事实上,我什至没有看到它们。

尝试使用 .NET 客户端(“添加服务引用”)发出此调用,并使用Fiddler 或类似的东西在线观看,看看发生了什么。然后做同样的事情。

【讨论】:

  • 非常感谢,我刚刚将GET 更改为POST,现在可以正常使用了。
猜你喜欢
  • 2014-03-26
  • 2015-11-20
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2011-07-21
  • 1970-01-01
  • 2010-10-10
相关资源
最近更新 更多