Simplest SOAP example

前端 未结 13 1879
死守一世寂寞
死守一世寂寞 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

    There are many quirks in the way browsers handle XMLHttpRequest, this JS code will work across all browsers:
    https://github.com/ilinsky/xmlhttprequest

    This JS code converts XML into easy to use JavaScript objects:
    http://www.terracoder.com/index.php/xml-objectifier

    The JS code above can be included in the page to meet your no external library requirement.

    var symbol = "MSFT"; 
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
    xmlhttp.onreadystatechange=function() {
     if (xmlhttp.readyState == 4) {
      alert(xmlhttp.responseText);
      // http://www.terracoder.com convert XML to JSON 
      var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
      var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
      // Result text is escaped XML string, convert string to XML object then convert to JSON object
      json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
      alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text); 
     }
    }
    xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    var xml = '' +
     '' + 
       ' ' +
         ' ' +
           '' + symbol + ' ' +
         ' ' +
       ' ' +
     '';
    xmlhttp.send(xml);
    // ...Include Google and Terracoder JS code here...
    

    Two other options:

    • JavaScript SOAP client:
      http://www.guru4.net/articoli/javascript-soap-client/en/

    • Generate JavaScript from a WSDL:
      https://cwiki.apache.org/confluence/display/CXF20DOC/WSDL+to+Javascript

提交回复
热议问题