Link to home
Start Free TrialLog in
Avatar of stormist
stormist

asked on

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:
https://www.experts-exchange.com/questions/21817159/XML-web-services-to-retrieve-areacode-zipcode.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>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of stormist
stormist

ASKER

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?

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