July 15, 2002 - Calling Web Services from JavaScript | WebReference

July 15, 2002 - Calling Web Services from JavaScript

Yehuda Shiran July 15, 2002
Calling Web Services from JavaScript
Tips: July 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

When you want to utilize your Web services, put them (asmx files) in a sub-directory of your Web server, such as c:\inetpub\wwwroot\Webreference. Then, when you access the services in your scripts, you refer to their directory as https://localhost/Webreference. For each Web service you utilize you need to have three JavaScript functions:

init() function assigns a short name to the Web service URL
  • A function to call the Web service with the appropriate parameters
  • A function to display the results
  • For our add Web service, the function addNumbers() is invoked when the Add button is clicked. It calls the webservice behavior's callService() method. The parameters are: (i) the function that handles the result display, (ii) the Web service command, "add", (iii) the first number, and (iv) the second number:

      function addNumbers(a, b) {
        myWebService.simpleCalcWebService.callService(addResult, 
          "add", first.value, second.value);
      }
    Similarly, for the IsPrime Web service, isPrimeNumber() is called when the Click here to check if prime button is clicked. It calls the Web service with three parameters: (i) the function to handle the result, isPrimeNumberResult, (ii) the Web service request, IsPrime, and (iii) the parameter expected by the Web service, testValue.value:

      function isPrimeNumber() {
        myWebService.isPrimeNumberWebService.callService(isPrimeNumberResult, 
          "IsPrime", 
          testValue.value);
      }
    To learn more about JScript .NET, go to Column 112, JScript .NET, Part VI: Creating IE Web Services.