Click here to Skip to main content
11,409,532 members (62,205 online)
Click here to Skip to main content
Articles » Languages » XML » Web Services » Downloads
Add your own
alternative version

A proxy generator to WebServices for JavaScript and AJAX

, 20 Sep 2005
Calling a server from JavaScript is a fundamental part of AJAX applications. Using WebServices with SOAP and WSDL is easy if proxy objects and methods are available in the browser.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html xmlns="http://schemas.microsoft.com/intellisense/html-401">

<head>
  <title>Prime factors calculator using WebServices for JavaScript</title>
  <script type="text/javascript" src="ajax.js"></script>
  <script type="text/javascript" src="GetJavaScriptProxy.aspx?service=CalcService.asmx"></script>
</head>

<body>
  <h1>Prime factors calculator using WebServices for JavaScript</h1>
  <table>
    <tbody>
      <tr>
        <th><label for="inputField">Please enter a number:</label></th>
        <td>
          <input id="inputField" onkeyup="StartCalcPrimeFactors()"></td>
        <td style="padding-left: 12px">
          <button onclick="proxies.EnableCache(proxies.CalcService.CalcPrimeFactors)">enable caching</button></td>
      </tr>
      <tr>
        <th><label>The factors are:</label></th>
        <td>
          <input id="outputField" size="60" disabled="disabled"></td>
      </tr>
    </tbody>
  </table>
  <h3>Hint:</h3>
  <p>try 12313123123123 or 12313123123123123123 for long running calculations ! </p>
  <script defer="defer" type="text/javascript">

// to set up a debug output in an alert box use:
// proxies.CalcService.CalcPrimeFactors.corefunc = proxies.alertResult;

// to set up a debug output of the http response body in an alert box use:
// proxies.CalcService.CalcPrimeFactors.corefunc = proxies.alertResponseText;

// attach window.alert for displaying result to make the direct webservice call asynchronous
// proxies.CalcService.CalcPrimeFactors.func = window.alert;

// alert any exceptions...
// proxies.CalcService.CalcPrimeFactors.onException = proxies.alertException;


var _num = "";
var _timer = null;

function StartCalcPrimeFactors() {
  var inputText = document.getElementById("inputField").value;
  if (_num != inputText) {
    if (_timer != null)
      window.clearTimeout(_timer);
    document.getElementById("outputField").value = "wait...";
    _num = inputText;
    // wait 300 msec. before continuing. This allows further typing...
    _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
  } // if
}

// calc prime factors
function CalcPrimeFactors() {
  var inputText = document.getElementById("inputField").value;

  if ((inputText == null) || (inputText.length == 0) || (inputText == "0"))
    return; // need no calculation 

  if (proxies.IsActive()) {
    // try again later...
    _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
    return;
  } // if

  proxies.CalcService.CalcPrimeFactors.func = displayFactors;  // hook up a method that gets the response
  proxies.CalcService.CalcPrimeFactors(inputText); // now call the server
} // CalcPrimeFactors


// The return value is passed to this function as a parameter
function displayFactors (retVal) {
  document.getElementById("outputField").value = retVal;
} // displayFactors</pre>

  </script>
  <hr />
  <p>This sample uses the WebServices for JavaScript on the client to call a WebService that calculates
    the prime numbers of a given number.</p>
  <p>This page is part of the <a href="http://ajaxaspects.blogspot.com/">http://ajaxaspects.blogspot.com/</a>
    project.</p>
  <hr />
</body>

</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Matthias Hertel
Architect Deutsche Bank AG
Germany Germany
see http://www.mathertel.de
Follow on   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 20 Sep 2005
Article Copyright 2005 by Matthias Hertel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid