views:

8614

answers:

7

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
+2  A: 

What about using a SOAP library such as this?

tim_yates
A good idea normally but I wanted something to improve understanding :)
Thomas Bratt
Well, get that lib and open it up - JavaScript *is* open source, after all...
Jason Bunting
OK but I'm hoping for the simplest example. I think it would benefit us to have a minimal example to learn from.
Thomas Bratt
hm, broken link.
Jweede
+2  A: 

Simplest example would consist of:

  1. Getting user input.
  2. Composing XML SOAP message similar to this

    <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/"&gt;
      <soap:Body>
        <GetInfoByZIP xmlns="http://www.webserviceX.NET"&gt;
          <USZip>string</USZip>
        </GetInfoByZIP>
      </soap:Body>
    </soap:Envelope>
    
  3. POSTing message to webservice url using XHR

  4. Parsing webservice's XML SOAP response similar to this

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     <soap:Body>
      <GetInfoByZIPResponse xmlns="http://www.webserviceX.NET"&gt;
       <GetInfoByZIPResult>
        <NewDataSet xmlns="">
         <Table>
          <CITY>...</CITY>
          <STATE>...</STATE>
          <ZIP>...</ZIP>
          <AREA_CODE>...</AREA_CODE>
          <TIME_ZONE>...</TIME_ZONE>
         </Table>
        </NewDataSet>
       </GetInfoByZIPResult>
      </GetInfoByZIPResponse>
     </soap:Body>
    </soap:Envelope>
    
  5. Presenting results to user.

But it's a lot of hassle without external JavaScript libraries.

Constantin
Not a Javacript example.
Thomas Bratt
+5  A: 

This cannot be done with straight JavaScript unless the web service is on the same domain as your page.

If the web service is on another domain 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 are going to do that then 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. Only 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.

Prestaul
The intention is to have the SOAP server also serve an HTML page for simple test and evaluation. The client would be on the same domain.Not using SOAP for the front end seems to be the accepted view. Any comments as to why? Please add to new question: http://stackoverflow.com/questions/127038
Thomas Bratt
No point in answering there... I agree with Gizmo on all three points. XML is bloated and a challenge to handle with js while JSON is concise and native.
Prestaul
re "cannot be done": Today it can be done with (mostly) straight JavaScript, if the client supports [Cross-Origin Resource Sharing](http://www.w3.org/TR/cors/). Hopefully in 3-4 years it will be universally available.
Constantin
A: 

SOAP is best used between middle tier type applications.

It is not good at, and has never been touted for front end use.

FlySwat
The intention is to have the SOAP server also serve an HTML page for simple test and evaluation. The client would be on the same domain.Not using SOAP for the front end seems to be the accepted view. Any comments as to why? Please add to new question: http://stackoverflow.com/questions/127038
Thomas Bratt
A: 

Thomas:

JSON is preferred for front end use because it is javascript. Therefore you have no XML to deal with. SOAP is a pain without using a library because of this. Somebody mentioned SOAPClient, which is a good library, we started with it for our project. However it had some limitations and we had to rewrite large chunks of it. It's been released as SOAPjs and supports passing complex objects to the server, and includes some sample proxy code to consume services from other domains.

Richard June
+4  A: 

There are many quirks in the way browsers handle XMLHttpRequest, this JS code will work across all browsers:
http://code.google.com/p/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 = '<?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/"&gt;' + 
   '<soap:Body> ' +
     '<GetQuote xmlns="http://www.webserviceX.NET/"&gt; ' +
       '<symbol>' + symbol + '</symbol> ' +
     '</GetQuote> ' +
   '</soap:Body> ' +
 '</soap:Envelope>';
xmlhttp.send(xml);
// ...Include Google and Terracoder JS code here...

Two other options:

Chris Stuart
Excellent answer, thanks :)
Thomas Bratt
+1  A: 

Easily consume SOAP Web services with JavaScript

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