Implementing an XML SOAP request using Javascript - problems with code

Posted on 2006-04-18
Last Modified: 2010-10-05
I've been trying to implement a basic XML request using javascript. I've pieced together bits of code though they are obviously not working. I am trying to retrieve the example request mentioned by Gertone in the following previous thread:

He was kind enough to show me how to retrieve a zipcode by city using XMLspy with these directions:
- start XMLSpy
    + in the SOAP menu pick "Create new soap request"
    + in "enter the WSDL..." put this wsdl address: ""
    + press OK
    + you will get four operations listed
    + double click "GetInfoByCity"
    + you will now see a soap XML file... that is the mock-up of what you have to send to the service
    + edit the XML, to fill in a city: eg. change <m:USCity>String</m:USCity> to <m:USCity>Washington</m:USCity>
    + in the SOAP menu "send request to server"

Now here is my attempt at code to do the same thing, I'm messing up really bad somewhere though can someone please clarify this coding and what I'm doing wrong:

<SCRIPT type="text/javascript">
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      try {
            xmlhttp = new XMLHttpRequest();
      } catch (e) {
if (!xmlhttp && window.createRequest) {
      try {
            xmlhttp = window.createRequest();
      } catch (e) {
}"GET", "",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
 xmlhttp.send(<SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:SOAP-ENC="" xmlns:xsi="" xmlns:xsd="">
            <m:GetInfoByCity xmlns:m="http://www.webserviceX.NET">


Question by:stormist
    LVL 60

    Accepted Solution

    The major part of your code is making sure it works accross browsers
    I recommend you to skip that part and try to make it work for one browser
    you can do the cross browser part later

    here is a version for FireFox

    <script language="javascript">

    function makeSoapRequest(strLoad)
      // create the payload of the SOAP request
      var soapData = '<SOAP-ENV:Envelope \n' +
                     'xmlns:SOAP-ENV="" \n' +
                     'xmlns:SOAP-ENC="" \n' +
                     'xmlns:xsi="" \n' +
                     'xmlns:xsd="">\n' +
                     '<SOAP-ENV:Body>\n' +
                     strLoad +
                     '</SOAP-ENV:Body>\n' +

       catch (e)
              alert("Permission UniversalBrowserRead denied.");
           var req = new XMLHttpRequest();
 "POST", "", false);
           req.setRequestHeader("Content-Length", soapData.length);
           req.setRequestHeader("Content-Type", "text/xml");
           req.setRequestHeader("Soapaction", "http://www.webserviceX.NET/GetInfoByCity");
           var soapResult = req.responseText;

          return soapResult;

                function showResult()
                         var xmlLoad = '<m:GetInfoByCity xmlns:m="http://www.webserviceX.NET">\n\t<m:USCity>McAllen</m:USCity>\n</m:GetInfoByCity>'
    <body onload="showResult();">
    LVL 60

    Expert Comment

    by:Geert Bormans
    important to note

    in order to be able to use soap requests from local files, you need the part on

    the actual request object is XMLHttpRequest() (that one is different in IE)
    you need POST instead of GET for SOAP,
    you don't need the WSDL's location but the service location as the second argument
    and I have a false there so it is synchronuous, saves us the onreadystatechange bit
    (you can change that later to true and have the onreadystatechange bit in, but easy things first)
    then you have to set some http request headers
    Content length is the length of the soapData (message constructed from the wrapper and the dynamic load)
    Content-type is text/xml
    Soapaction is found as I told you before in XML Spy
    Then I make the call
    Then I catch the result in req.responseText (the string version of the response XML)

    I have a function showResult that is called on body load

    there is an alert for the soap message
    and an alert for the result

    I bet you can stuff it on the page yourself

    good luck

    LVL 10

    Author Comment

    Thank you very much, I know you can't walk me through each stage but if I could ask a couple of things really quick:
    why does that security box pop-up? What file are we accessing on the local machine, I thought it was all on the xml web server
    is it possible to parse this file into HTML using XSLT without actually saving the return data into a file?

    LVL 10

    Author Comment

    I've learned by reading ajax websites that you can't access remote servers because of the sandbox security and you have to use a proxy to accomplish this without the securtiy alert. Any idea how to do that? I'm still unclear on the parsing the data, I've seen dozens of functions but I'm trying to narrow it down to one I can use. I'm actually trying to apply this to a real-world problem. We've got a website and I need to query zip area etc based upon areacode, but I need to turn these results into managable variables. Any other gems of knowledge would be much appreciated!
    LVL 60

    Expert Comment

    by:Geert Bormans
    I missed your posts last night (it was 2 AM here :-)
    sorry for letting you wait

    > why does that security box pop-up?

    because of the
    just press "allow" and check the "remember decission"

    > What file are we accessing on the local machine
    this html file, or do you serve it up?
    if you serve it up, you can leave the security bit out

    > is it possible to parse this file into HTML using XSLT without actually saving the return data into a file?

    yes  of course, that also is browser depending... it might be worthwhile to paste the javascript in an ASP page
    and handle all the transformation and soap messaging server site instead of client side
    (browser independent SOAP and XSLT is a pain, you will spend half of the code base checking for existing objects)

    > use a proxy to accomplish this without the securtiy alert

    you need to setup a proxy server ( google around to find a good one, maybe your server already has one)
    the proxy setting is just another property on the http request

    > I'm still unclear on the parsing the data

    you could use selectNodes or selectSingleNode with an XPath
    strZip = selectNodes("//Table[1]/ZIP").text;
    would give you the zip code found in the first table
    (it could be that you need to replace .text with .value, I am not sure)

    have fun,


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now