Trying To Invoke a WebService

Discussion in 'Javascript' started by Wayne Wengert, Aug 13, 2005.

  1. I found a code sample that allows you to use a 5 digit zip code to lookup
    the correct city and state using a Web Service (ASP.NET). When I run the
    javascript code (see below) on my aspx page neither the "if
    (window.XMLHttpRequest)" nor the " else if (window.ActiveXObject) " is true?
    I tried viewing those objects status but they do not appear to be valid
    objects in ASP.NET. How can I accomplish this?

    Wayne

    ================= code ==============
    <script language="Javascript">
    <!--

    var req;
    var response;
    var city;
    var state;

    function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object

    if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    isIE = true;
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send();
    }
    }
    }

    function processReqChange() {
    if (req.readyState == 4) {
    if (req.status == 200) {
    document.forms[0].output.value = req.responseText
    response = req.responseXML.documentElement;
    city = response.getElementsByTagName('city')[0].firstChild.data;
    state = response.getElementsByTagName('state')[0].firstChild.data;
    alert(city);
    document.forms[0].txtcity.value = city
    document.forms[0].txtstate.value = state
    } else {
    alert("Please enter a valid zip code:\n" +
    req.statusText);
    }
    }
    }

    function loadxml(form) {
    loadXMLDoc('http://www.wengert.org/' +
    'zipcodes.asmx/GetZip?z=' + document.forms[0].txtZIP.value);
    }

    //-->

    </script>
     
    Wayne Wengert, Aug 13, 2005
    #1
    1. Advertisements

  2. Wayne Wengert

    McKirahan Guest

    I don't know about ASP.NET but this works for me:

    <html>
    <head>
    <title>xml_zip.htm</title>
    <script type="text/javascript">
    var req;
    var response;
    var city;
    var state;

    function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    isIE = true;
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send();
    }
    }
    }

    function processReqChange() {
    if (req.readyState == 4) {
    if (req.status == 200) {
    document.forms[0].output.value = req.responseText
    response = req.responseXML.documentElement;
    city = response.getElementsByTagName('city')[0].firstChild.data;
    state =
    response.getElementsByTagName('state')[0].firstChild.data;
    alert(city + ", " + state);
    document.forms[0].txtcity.value = city
    document.forms[0].txtstate.value = state
    } else {
    alert("Please enter a valid zip code:\n" + req.statusText);
    }
    }
    }

    function loadxml() {
    loadXMLDoc('http://www.wengert.org/zipcodes.asmx/GetZip?z=' +
    document.forms[0].txtZIP.value);
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="txtZIP" size="5" maxlength="5" value="">
    <input type="button" value="Lookup" onclick="loadxml()">
    <br>City, State :
    <input type="text" name="txtcity">
    <input type="text" name="txtstate">
    <br>
    <textarea name="output" cols="100" rows="30"></textarea>
    </form>
    </body>
    </html>


    I changed your
    function loadxml(form) {
    to
    function loadxml() {
    as well as the indentation for (my) readability.
     
    McKirahan, Aug 13, 2005
    #2
    1. Advertisements

  3. Thanks for the response. I too can get it to work if I use an old style
    (ASP) form but it appears that the 2 objects I mentioned are not valid in an
    ASP.NET environment?

    Wayne

     
    Wayne Wengert, Aug 13, 2005
    #3
  4. What you have below is client-side JavaScript code which is executed in
    the browser and has no relevance to ASP.NET which is executed on the
    server. Of course XMLHttpRequest respectively Microsoft.XMLHTTP are
    used to load data from a HTTP server but whether that server runs
    classic ASP or ASP.NET or JSP or PHP or no server side scripting
    framework at all is not relevant to using XMLHttpRequest or
    Microsoft.XMLHTTP on the client. So the statement "they do not appear to
    be valid objects in ASP.NET" does not really make sense, the client has
    to support the objects and XMLHttpRequest is supported by Mozilla,
    Netscape 7/8, Opera 8, Safari 1.2, Konqueror 3.3, and Microsoft.XMLHTTP
    by IE 5 and later on Windows.
    If something does not work as you want then it can have many reasons, if
    you use Mozilla or Firefox check the JavaScript console first.
    You need to be aware that with normal security settings XMLHttpRequest
    can load data only from the same HTTP server as the HTML document with
    the script has been loaded from. So perhaps you are trying to access
    that web service on www.wengert.org with a script in a HTML document
    loaded from another server and then simply get no access.
     
    Martin Honnen, Aug 13, 2005
    #4
  5. Marttin;

    Thanks for the response. I think I need to do some studying to get an
    understanding of all the items you mentioned.

    Wayne
     
    Wayne Wengert, Aug 13, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.