spacer

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

home / experts / javascript / column97


Web Services, Part II: Calling Service Methods

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Sending A Message to the Web Service

In order to communicate with Web Services, you need to call the callService() method. The callService() method has an optional first parameter of a callback function, followed by the WebService's method name and its parameter values. Here is the syntax:

iCallID = id.FriendlyName.callService([CallbackHandler,]
  "MethodName",
  Param1, Param2, ...);

For example:

webServiceCallerBody.echo.callService(Handler,
  "echoString", "Asynchronous Call");

The callback handler function processes the result sent by the Web service. If the callback handler function is not specified, the event handler of the onresult event is used.

If you choose to call the callService() method without a callback handler, then you need to assign the onresult event handler instead:

iCallID = webServiceCallerBody.echo.callService
  ("echoString", "Asynchronous Call");

You can also call it with an object as the first parameter:

oResult = webServiceCallerBody.echo.callService
  (co, "Synchronous Call");

The object co is created by the following constructor function:

function createCallOptions(fn, pn, cm, to, un, pw,
  hd, ep, pr) {
  var o = new Object();
  o.funcName = fn;
  o.portName = pn;
  o.async    = cm;
  o.timeout  = to;
  o.userName = un;
  o.password = pw;
  o.SOAPHeader= hd;
  o.endpoint = ep;
  o.params   = pr;
  return o;
}

The async property above enables two ways to call a Web service, asynchronous and synchronous. The asynchronous way is the default. You just call the Web service and don't wait for a return, like in this example:

iCallID = webServiceCallerBody.echo.callService(
  handleResult, "echoString", "Asynchronous Call");

You don't wait for the Web service to return its answer. When it returns, the handleResult() function handles the value coming back:

function handleResult(res) {
  if (!res.error) {
    alert("Successful call. Result is " + res.value);
  }
  else {
    alert("Unsuccessful call. Error is " +
      res.errorDetail.string);
  }
}

The synchronous way kicks in when you set the async property of the Call Options object to false. This is how you create the co object and set its async property:

function callSynch() {
  var co = webServiceCallerBody.createCallOptions();
  co.funcName = "echoString";
  co.async = false;
  var oResult = webServiceCallerBody.echo.callService
    (co, "Synchronous Call");
  handleResult(oResult);
}

Next: How to handle the response through the result object

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: November 19, 2001
Revised: November 19, 2001

URL: http://www.webreference.com/js/column97/4.html