Simplest SOAP example

前端 未结 13 1657
死守一世寂寞
死守一世寂寞 2020-11-22 01:32

What is the simplest SOAP example using Javascript?

To be as useful as possible, the answer should:

  • Be functional (in other words actually work)
  • <
相关标签:
13条回答
  • 2020-11-22 02:04

    Easily consume SOAP Web services with JavaScript -> Listing B

    function fncAddTwoIntegers(a, b)
    {
        varoXmlHttp = new XMLHttpRequest();
        oXmlHttp.open("POST",
     "http://localhost/Develop.NET/Home.Develop.WebServices/SimpleService.asmx'",
     false);
        oXmlHttp.setRequestHeader("Content-Type", "text/xml");
        oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/AddTwoIntegers");
        oXmlHttp.send(" \
    <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> \
        <AddTwoIntegers xmlns='http://tempuri.org/'> \
          <IntegerOne>" + a + "</IntegerOne> \
          <IntegerTwo>" + b + "</IntegerTwo> \
        </AddTwoIntegers> \
      </soap:Body> \
    </soap:Envelope> \
    ");
        return oXmlHttp.responseXML.selectSingleNode("//AddTwoIntegersResult").text;
    }
    

    This may not meet all your requirements but it is a start at actually answering your question. (I switched XMLHttpRequest() for ActiveXObject("MSXML2.XMLHTTP")).

    0 讨论(0)
  • 2020-11-22 02:05

    This is the simplest JavaScript SOAP Client I can create.

    <html>
    <head>
        <title>SOAP JavaScript Client Test</title>
        <script type="text/javascript">
            function soap() {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open('POST', 'https://somesoapurl.com/', true);
    
                // build SOAP request
                var sr =
                    '<?xml version="1.0" encoding="utf-8"?>' +
                    '<soapenv:Envelope ' + 
                        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                        'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
                        'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                        'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                        '<soapenv:Body>' +
                            '<api:some_api_call soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
                                '<username xsi:type="xsd:string">login_username</username>' +
                                '<password xsi:type="xsd:string">password</password>' +
                            '</api:some_api_call>' +
                        '</soapenv:Body>' +
                    '</soapenv:Envelope>';
    
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4) {
                        if (xmlhttp.status == 200) {
                            alert(xmlhttp.responseText);
                            // alert('done. use firebug/console to see network response');
                        }
                    }
                }
                // Send the POST request
                xmlhttp.setRequestHeader('Content-Type', 'text/xml');
                xmlhttp.send(sr);
                // send request
                // ...
            }
        </script>
    </head>
    <body>
        <form name="Demo" action="" method="post">
            <div>
                <input type="button" value="Soap" onclick="soap();" />
            </div>
        </form>
    </body>
    </html> <!-- typo -->
    
    0 讨论(0)
  • 2020-11-22 02:08

    Some great examples (and a ready-made JavaScript SOAP client!) here: http://plugins.jquery.com/soap/

    Check the readme, and beware the same-origin browser restriction.

    0 讨论(0)
  • 2020-11-22 02:12
    function SoapQuery(){
      var namespace = "http://tempuri.org/";
      var site = "http://server.com/Service.asmx";
      var xmlhttp = new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");
      xmlhttp.setOption(2,  13056 );  /* if use standard proxy */
      var args,fname =  arguments.callee.caller.toString().match(/ ([^\(]+)/)[1]; /*Имя вызвавшей ф-ции*/
      try { args =   arguments.callee.caller.arguments.callee.toString().match(/\(([^\)]+)/)[1].split(",");  
        } catch (e) { args = Array();};
      xmlhttp.open('POST',site,true);  
      var i, ret = "", q = '<?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><'+fname+ ' xmlns="'+namespace+'">';
      for (i=0;i<args.length;i++) q += "<" + args[i] + ">" + arguments.callee.caller.arguments[i] +  "</" + args[i] + ">";
      q +=   '</'+fname+'></soap:Body></soap:Envelope>';
                // Send the POST request
                xmlhttp.setRequestHeader("MessageType","CALL");
                xmlhttp.setRequestHeader("SOAPAction",namespace + fname);
                xmlhttp.setRequestHeader('Content-Type', 'text/xml');
                //WScript.Echo("Запрос XML:" + q);
                xmlhttp.send(q);
         if  (xmlhttp.waitForResponse(5000)) ret = xmlhttp.responseText;
        return ret;
      };
    
    
    
    
    
    function GetForm(prefix,post_vars){return SoapQuery();};
    function SendOrder2(guid,order,fio,phone,mail){return SoapQuery();};
    
    function SendOrder(guid,post_vars){return SoapQuery();};
    
    0 讨论(0)
  • 2020-11-22 02:15

    The question is 'What is the simplest SOAP example using Javascript?'

    This answer is of an example in the Node.js environment, rather than a browser. (Let's name the script soap-node.js) And we will use the public SOAP web service from Europe PMC as an example to get the reference list of an article.

    const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    const DOMParser = require('xmldom').DOMParser;
    
    function parseXml(text) {
        let parser = new DOMParser();
        let xmlDoc = parser.parseFromString(text, "text/xml");
        Array.from(xmlDoc.getElementsByTagName("reference")).forEach(function (item) {
            console.log('Title: ', item.childNodes[3].childNodes[0].nodeValue);
        });
    
    }
    
    function soapRequest(url, payload) {
        let xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', url, true);
    
        // build SOAP request
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    parseXml(xmlhttp.responseText);
                }
            }
        }
    
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.send(payload);
    }
    
    soapRequest('https://www.ebi.ac.uk/europepmc/webservices/soap', 
        `<?xml version="1.0" encoding="UTF-8"?>
        <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Header />
        <S:Body>
            <ns4:getReferences xmlns:ns4="http://webservice.cdb.ebi.ac.uk/"
                xmlns:ns2="http://www.scholix.org"
                xmlns:ns3="https://www.europepmc.org/data">
                <id>C7886</id>
                <source>CTX</source>
                <offSet>0</offSet>
                <pageSize>25</pageSize>
                <email>ukpmc-phase3-wp2b---do-not-reply@europepmc.org</email>
            </ns4:getReferences>
        </S:Body>
        </S:Envelope>`);
    

    Before running the code, you need to install two packages:

    npm install xmlhttprequest
    npm install xmldom
    

    Now you can run the code:

    node soap-node.js
    

    And you'll see the output as below:

    Title:  Perspective: Sustaining the big-data ecosystem.
    Title:  Making proteomics data accessible and reusable: current state of proteomics databases and repositories.
    Title:  ProteomeXchange provides globally coordinated proteomics data submission and dissemination.
    Title:  Toward effective software solutions for big biology.
    Title:  The NIH Big Data to Knowledge (BD2K) initiative.
    Title:  Database resources of the National Center for Biotechnology Information.
    Title:  Europe PMC: a full-text literature database for the life sciences and platform for innovation.
    Title:  Bio-ontologies-fast and furious.
    Title:  BioPortal: ontologies and integrated data resources at the click of a mouse.
    Title:  PubMed related articles: a probabilistic topic-based model for content similarity.
    Title:  High-Impact Articles-Citations, Downloads, and Altmetric Score.
    
    0 讨论(0)
  • 2020-11-22 02:17
    <html>
     <head>
        <title>Calling Web Service from jQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#btnCallWebService").click(function (event) {
                    var wsUrl = "http://abc.com/services/soap/server1.php";
                    var soapRequest ='<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> <getQuote xmlns:impl="http://abc.com/services/soap/server1.php">  <symbol>' + $("#txtName").val() + '</symbol>   </getQuote> </soap:Body></soap:Envelope>';
                                   alert(soapRequest)
                    $.ajax({
                        type: "POST",
                        url: wsUrl,
                        contentType: "text/xml",
                        dataType: "xml",
                        data: soapRequest,
                        success: processSuccess,
                        error: processError
                    });
    
                });
            });
    
            function processSuccess(data, status, req) { alert('success');
                if (status == "success")
                    $("#response").text($(req.responseXML).find("Result").text());
    
                    alert(req.responseXML);
            }
    
            function processError(data, status, req) {
            alert('err'+data.state);
                //alert(req.responseText + " " + status);
            } 
    
        </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" ></div>
    </body>
    </html>
    

    Hear is best JavaScript with SOAP tutorial with example.

    http://www.codeproject.com/Articles/12816/JavaScript-SOAP-Client

    0 讨论(0)
提交回复
热议问题