spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column106


Web Services, Part XI: Consuming Multiple Web Services

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

Scripting A Multiple-Service Consumer

You need to have three JavaScript functions for each Web service you consume:

  • The 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

The init() function can be shared among Web services. It is invoked upon loading of the page, onload=init(). We call the behavior's useService() method to assign short identifiers to the Web service's rather-long URL. We call the Add Web service by "simpleCalcWebService" and the isPrime Web service by "isPrimeNumberWebService":


function init() {
  myWebService.useService("
    http://www.dev1.eraserver.net/D2S/WebServiceSample/
      isPrime.asmx?WSDL",
    "isPrimeNumberWebService");
  myWebService.useService("http://63.210.240.215/d2s/
    20011205/add.asmx?WSDL",
    "simpleCalcWebService");
}

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, 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);
}

We use DIV elements to display results of both Web services. Once a result is ready, we update the innerHTML property of the appropriate element. Here is how we update the result of the Add Web service:

function addResult(result) {
  theResult1.innerHTML = result.value;
}

We embed the result of the IsPrime Web service within a descriptive message:

function isPrimeNumberResult(result) {
  theResult2.innerHTML = "The number " + testValue.value +
    (result.value ? " is" : " is not") + " a prime number";
}

Next: How to consume the Add Web service

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2 · Create Multilingual Web Sites with Windows Unicode Fonts
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MySql View Technique for Grouping Crosstab Column Data · Why You Need a Mobile Web Site · Tame Web Marketing with Social Media Management


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: March 25, 2002
Revised: March 25, 2002

URL: http://www.webreference.com/js/column106/3.html