使用包含JSON Payload的SOAP WebService

时间:2013-12-17 14:25:12

标签: jquery ajax json web-services soap

我正在尝试编写一个WebApp,它使用嵌入JSON数据的SOAP Web服务,并且遇到了挑战。我很感激这方面的帮助。谢谢。

以下是Web服务请求的预期结构:

<logonrequest>
    {
    "userid":"username",
    "password":"password",
    "Timestamp":""
    }
</logonrequest>

以下是预期的Web服务响应的结构:

<return>
    {
        "userid":"username",
        "displayName":"Firstname Lastname",
        "accountBalance":0.0,
        "statusCode":"0|SUCCESS",
        "statusDesc":"Logon Success",
        "sessionid":"12047CC560484D21BD328EC8F22814ED",
        "lastTransactionTime":"20131130234326"
    }
</return>

以下是我的代码:

 <html>
 <head>
    <title>Calling Web Service from jQuery</title>
    <script type="text/javascript" src="./js/jquery.js"></script>
    <script type="text/javascript" src="./js/jquery.soap.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                $.soap({
                    url: 'URL GOES HERE',
                    elementName: 'logonrequest',
                    appendMethodToURL: false,
                    soap12: false,
                    data: "{'userid':'bernard','password':'password','Timestamp':''}",
                    // data: {
                    //     'logonrequest': "{'userid':'bernard','password':'password','Timestamp':''}"
                    // },
                    beforeSend: function (SOAPRequest)  {
                        alert('Request: ' + SOAPRequest);
                    },
                    success: function (soapResponse) {
                        alert('0: success');
                    },
                    error: function (SOAPResponse) {
                        alert('1: failed');
                    }
                });
            });
        });
    </script>
</head>

<body>
    <h3>
        Calling Web Services with jQuery/AJAX
    </h3>
    Enter your name:
    <input id="txtName" type="text" />
    <input id="btnCallWebService" value="Call web service" type="button" />
    <div id="response" />
</body>
</html>

使用此:

 elementName: 'logonrequest',
 ...
 data: "{'userid':'bernard','password':'password','Timestamp':''}"

我得到了

Error: Invalid XML: {'userid':'bernard','password':'password','Timestamp':''}

使用此:

 elementName: 'logonrequest' OR method: 'logonrequest',
 ...
 data: {
     'logonrequest': "{'userid':'bernard','password':'password','Timestamp':''}"
 }

请求失败,因为实际创建的请求是:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <logonrequest>
            <logonrequest>
                {
                    'userid':'bernard',
                    'password':'password',
                    'Timestamp':''
                }
            </logonrequest>
        </logonrequest>
    </soap:Body>
 </soap:Envelope>

如何让它发送一个看起来与上面所需的样本请求完全相同的请求才能成功点击服务?谢谢。

1 个答案:

答案 0 :(得分:0)

我能在请求方面实现您想要的唯一方法就是使用它:

    <html>
 <head>
    <title>Calling Web Service from jQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="./js/jquery.soap.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                $.soap({
                    url: 'URL GOES HERE',
                    appendMethodToURL: false,
                    soap12: false,
                    data: "<logonrequest>{'userid':'bernard','password':'password','Timestamp':''}</logonrequest>",
                    // data: {
                    //     'logonrequest': "{'userid':'bernard','password':'password','Timestamp':''}"
                    // },
                    beforeSend: function (SOAPRequest)  {
                        alert('Request: ' + SOAPRequest);
                    },
                    success: function (soapResponse) {
                        console.log(soapResponse);
                        alert('0: success');
                    },
                    error: function (SOAPResponse) {
                        alert('1: failed');
                    }
                });
            });
        });
    </script>
</head>

<body>
    <h3>
        Calling Web Services with jQuery/AJAX
    </h3>
    Enter your name:
    <input id="txtName" type="text" />
    <input id="btnCallWebService" value="Call web service" type="button" />
    <div id="response" />
</body>
</html>

我希望这就是你想要实现的目标。 注意我删除了elementName并将完整的xml请求添加到数据属性中。