Joseph Michael Pesch
VP Programming

HTML JavaScript Web Service Call

by 17. September 2010 14:56

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

  <script type="text/javascript">

    function MakeSoapCall(WebServiceUrl, Namespace, WebMethod, WebMethodParams) {

      var xmlHttp;

      function SoapCall() {

        // creatng the xmlHttp object

        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

        // Calling the web service using post and true means asynchronous call

        // WebServiceUrl Sample: http://localhost/MyWebService.asmx

        xmlHttp.open("post", WebServiceUrl, true);

        // Setting the request header to let the web service identify the soap request we would be sending

        xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        // SoapAction Sample: http://MyNameSpace.com/MyWebMethod – name of the webmethod

        // Example: [WebService(Namespace = "http://MyNameSpace.com/")] applied to our web service class

        xmlHttp.setRequestHeader("SOAPAction", "http://" + Namespace + "/" + WebMethod);

        xmlHttp.onreadystatechange = doUpdate;

        // setting the soap request body

        var soapRequest = "<?xml version='1.0' encoding='utf-8'?>" +

          "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +

          " xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +

          "<soap:Body>" +

          "<" + WebMethod + " xmlns='http://" + Namespace + "/'>" +

          WebMethodParams +

          "</" + WebMethod + ">" +

          " </soap:Body>" +

          "</soap:Envelope>";

        // send the request

        document.all('WebMethodParamsTextArea').value = soapRequest;

        xmlHttp.send(soapRequest);

        return false;

      }

      function doUpdate() {

        debugger;

        if (xmlHttp.readyState == 4) {

          var responseXMLResult = xmlHttp.responseXML;

          // var result = responseXMLResult.lastChild.nodeTypedValue;

          alert(responseXMLResult.xml);

        }

      }

      SoapCall();

    }

   

  </script>

</head>

<body>

  <form id="form1" runat="server">

  <div style="font-family: Calibri;">

    <table>

      <tr>

        <td>

          Web Service URL:

        </td>

        <td>

          <input type="text" id="WebServiceUrlControl" size="100" value="http://www.27seconds.com/Holidays/US/Dates/USHolidayDates.asmx" />

        </td>

      </tr>

      <tr>

        <td>

          Namespace:

        </td>

        <td>

          <input type="text" id="NamespaceControl" size="100" value="www.27seconds.com/Holidays/US/Dates" />

        </td>

      </tr>

      <tr>

        <td>

          Web Method:

        </td>

        <td>

          <input type="text" id="WebMethodControl" size="100" value="GetChristmasDay" />

        </td>

      </tr>

      <tr>

        <td valign="top">

          Web Method Paramaters:

        </td>

        <td>

          <textarea id="WebMethodParamsTextArea" rows="5" cols="75">

<year>2010</year>

          </textarea>

        </td>

      </tr>

      <tr>

        <td>

        </td>

        <td>

        </td>

      </tr>

    </table>

    <br />

    <input type="button" value="Make Soap Call" onclick="MakeSoapCall(document.all('WebServiceUrlControl').value, document.all('NamespaceControl').value, document.all('WebMethodControl').value, document.all('WebMethodParamsTextArea').value);" />

  </div>

  </form>

</body>

</html>

 

 

Tags:

HTML | JavaScript

Comments are closed