Web Services, Part II: Calling Service Methods: Sending A Message to the Web Service - Doc JavaScript | WebReference

Web Services, Part II: Calling Service Methods: Sending A Message to the Web Service - Doc JavaScript


Web Services, Part II: Calling Service Methods

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


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