且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用AJAX jQuery和SOAP调用WebService

更新时间:2022-05-30 05:28:57

您的问题在您的 soapRequest 中,完全是这一行:

Your issue is in your soapRequest, exactly this line:

<Add> \

打开此URL http://www.dneonline.com/calculator .asmx?op = Add ,您可以看到您的 Add 方法是:

Opening this url http://www.dneonline.com/calculator.asmx?op=Add you can see your Add method is:

<Add xmlns="http://tempuri.org/">

确实,您收到的错误消息是:

Indeed, the error message you receive is:

Unable to handle request without a valid action parameter. Please supply a valid soap action.

摘要:

// the following in order to handle cors in this demo....
jQuery.ajaxPrefilter(function(options) {
    if (options.crossDomain && jQuery.support.cors) {
        options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
    }
});

$("#BTNSERVICE").click(function (event) {
    var intA = $('#intA').val();
    var intB = $('#intB').val();

    var soapRequest = `<?xml version="1.0" encoding="utf-8"?>
                       <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                            <soap:Body>
                                <Add xmlns="http://tempuri.org/">
                                    <intA>${intA}</intA>
                                    <intB>${intB}</intB>
                                </Add>
                            </soap:Body>
                        </soap:Envelope>`;

    var webserUrl = "http://www.dneonline.com/calculator.asmx?WSDL";
    var self = this;
    self.disabled = true;
    $.ajax({
        type: "POST",
        url: webserUrl,
        dataType: "xml",
        processData: false,
        contentType: "text/xml; charset=\"utf-8\"",
        data: soapRequest,
        success: function (data, status, req) {
            if (status == "success") {
                var result = $(req.responseXML).find('AddResult').text();
                $('#AddResult').val(result);
            }
        },
        error: function (data, status, req) {
            $('#errmsg').text(data.responseText);
        },
        complete: function(data, status) {
            self.disabled = false;
        }
    });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<form runat="server">
    intA: <input id="intA" type="text" value="0"> <br>
    intB: <input id="intB" type="text" value="0"> <br>
    AddResult: <input id="AddResult" type="text" value="0"> <br>
    <button id="BTNSERVICE"  type="button">SAMPLE Application to test service</button>
</form>
<div id="errmsg"></div>

以下是包含代码的最小HTML文件.您可以在其中看到我在何处以及如何添加js和html表单元素:

A minimal html file containing the code follows. You can see in it where and how I added js and html form element:

  1. 在标题部分,您可以看到我如何添加jQuery库和js代码
  2. 在正文部分,您可以看到带有输入字段和按钮的表单元素.

以下是最小的html文件,其中仅包含您可以使用的js代码和html标签.

The following is the minimal html file containing only the js code and html tags you can use.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

    <!-- in the header section you can add javascript -->

    <!-- include the jQuery library version 3.3.1 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- include javascript code -->
    <script>
        // the following in order to handle cors in this demo....
        // for details see: https://github.com/Rob--W/cors-anywhere/#documentation
        jQuery.ajaxPrefilter(function (options) {
            if (options.crossDomain && jQuery.support.cors) {
                options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
            }
        });

        // on DOM ready...
        $(function () {
            // the following event handler will handle click events for BUTTON
            $("#BTNSERVICE").on('click', function (event) {
                var intA = $('#intA').val();
                var intB = $('#intB').val();

                var soapRequest = `<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                        <soap:Body>
                            <Add xmlns="http://tempuri.org/">
                                <intA>${intA}</intA>
                                <intB>${intB}</intB>
                            </Add>
                        </soap:Body>
                </soap:Envelope>`;

                var webserUrl = "http://www.dneonline.com/calculator.asmx?WSDL";
                var self = this;
                self.disabled = true;
                $.ajax({
                    type: "POST",
                    url: webserUrl,
                    dataType: "xml",
                    processData: false,
                    contentType: "text/xml; charset=\"utf-8\"",
                    data: soapRequest,
                    success: function (data, status, req) {
                        if (status == "success") {
                            var result = $(req.responseXML).find('AddResult').text();
                            $('#AddResult').val(result);
                        }
                    },
                    error: function (data, status, req) {
                        $('#errmsg').text(data.responseText);
                    },
                    complete: function (data, status) {
                        self.disabled = false;
                    }
                });
            });
        });
    </script>


</head>
<body>


<!-- in the body section you can add form -->
<form runat="server">
    intA: <input id="intA" type="text" value="0"> <br>
    intB: <input id="intB" type="text" value="0"> <br>
    AddResult: <input id="AddResult" type="text" value="0"> <br>
    <button id="BTNSERVICE" type="button">SAMPLE Application to test service</button>
</form>
<div id="errmsg"></div>


</body>
</html>