Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1705
  • Last Modified:

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:
http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21817159.html

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: "http://www.webservicex.net/uszip.asmx?WSDL"
    + 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:


<<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
</HEAD>
<BODY>
<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) {
            xmlhttp=false;
      }
}
if (!xmlhttp && window.createRequest) {
      try {
            xmlhttp = window.createRequest();
      } catch (e) {
            xmlhttp=false;
      }
}
 xmlhttp.open("GET", "http://www.webservicex.net/uszip.asmx?WSDL",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alert(xmlhttp.responseText)
  }
 }
 xmlhttp.send(<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SOAP-ENV:Body>
            <m:GetInfoByCity xmlns:m="http://www.webserviceX.NET">
                  <m:USCity>McAllen</m:USCity>
            </m:GetInfoByCity>
      </SOAP-ENV:Body>
</SOAP-ENV:Envelope>)


</SCRIPT>

</BODY>
</HTML>
0
stormist
Asked:
stormist
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
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

<html>
<head>
<script language="javascript">

function makeSoapRequest(strLoad)
  {
  // create the payload of the SOAP request
  var soapData = '<SOAP-ENV:Envelope \n' +
                 'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" \n' +
                 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" \n' +
                 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n' +
                 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">\n' +
                 '<SOAP-ENV:Body>\n' +
                 strLoad +
                 '</SOAP-ENV:Body>\n' +
                 '</SOAP-ENV:Envelope>\n';
                 alert(soapData);

   try
         {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
         }
   catch (e)
   {
          alert("Permission UniversalBrowserRead denied.");
   }
       var req = new XMLHttpRequest();
       req.open("POST", "http://www.webservicex.net/uszip.asmx", false);
       req.setRequestHeader("Content-Length", soapData.length);
       req.setRequestHeader("Content-Type", "text/xml");
       req.setRequestHeader("Soapaction", "http://www.webserviceX.NET/GetInfoByCity");
       req.send(soapData);
       
       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>'
                     alert(makeSoapRequest(xmlLoad));
                        }
</script>
</head>
<body onload="showResult();">
</body>
</html>
0
 
Geert BormansCommented:
important to note

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

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

Geert
0
 
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?

0
 
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!
0
 
Geert BormansCommented:
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 netscape.security.PrivilegeManager
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,

Geert
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now