Implementing an XML SOAP request using Javascript - problems with code

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">


LVL 10
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
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();">

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
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

stormistAuthor Commented:
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?

stormistAuthor Commented:
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!
Geert BormansInformation ArchitectCommented:
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,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.