Simplest SOAP example

05/23/2020 02:00:01

What is the simplest SOAP example using Javascript?

To be as useful as possible, the answer should:

  • Be functional (in other words actually work)
  • Send at least one parameter that can be set elsewhere in the code
  • Process at least one result value that can be read elsewhere in the code
  • Work with most modern browser versions
  • Be as clear and as short as possible, without using an external library

Verified Answer (200 Votes)

07/10/2012 03:30:04

This is the simplest JavaScript SOAP Client I can create.

    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
  'POST', '', true);

            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soapenv:Envelope ' + 
                    'xmlns:xsi="" ' +
                    'xmlns:api="" ' +
                    'xmlns:xsd="" ' +
                    'xmlns:soapenv="">' +
                    '<soapenv:Body>' +
                        '<api:some_api_call soapenv:encodingStyle="">' +
                            '<username xsi:type="xsd:string">login_username</username>' +
                            '<password xsi:type="xsd:string">password</password>' +
                        '</api:some_api_call>' +
                    '</soapenv:Body>' +

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        // alert('done. use firebug/console to see network response');
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            // send request
            // ...
    <form name="Demo" action="" method="post">
            <input type="button" value="Soap" onclick="soap();" />
</html> <!-- typo -->

Answer #2 (80 Votes)

11/08/2009 09:46:36

There are many quirks in the way browsers handle XMLHttpRequest, this JS code will work across all browsers:

This JS code converts XML into easy to use JavaScript objects:

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

var symbol = "MSFT"; 
var xmlhttp = new XMLHttpRequest();"POST", "",true);
xmlhttp.onreadystatechange=function() {
 if (xmlhttp.readyState == 4) {
  // 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 = '<?xml version="1.0" encoding="utf-8"?>' +
 '<soap:Envelope xmlns:xsi="" ' +
                'xmlns:xsd="" ' +
                'xmlns:soap="">' + 
   '<soap:Body> ' +
     '<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
       '<symbol>' + symbol + '</symbol> ' +
     '</GetQuote> ' +
   '</soap:Body> ' +
// ...Include Google and Terracoder JS code here...

Two other options:


Answer #3 (48 Votes)

09/24/2008 08:22:22

This cannot be done with straight JavaScript unless the web service is on the same domain as your page. Edit: In 2008 and in IE<10 this cannot be done with straight javascript unless the service is on the same domain as your page.

If the web service is on another domain [and you have to support IE<10] then you will have to use a proxy page on your own domain that will retrieve the results and return them to you. If you do not need old IE support then you need to add CORS support to your service. In either case, you should use something like the lib that timyates suggested because you do not want to have to parse the results yourself.

If the web service is on your own domain then don't use SOAP. There is no good reason to do so. If the web service is on your own domain then modify it so that it can return JSON and save yourself the trouble of dealing with all the hassles that come with SOAP.

Short answer is: Don't make SOAP requests from javascript. Use a web service to request data from another domain, and if you do that then parse the results on the server-side and return them in a js friendly form.

Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.