Web Services, Part II: Calling Service Methods: Handling the result Object - Doc JavaScript | WebReference

Web Services, Part II: Calling Service Methods: Handling the result Object - Doc JavaScript


Web Services, Part II: Calling Service Methods

Handling the result Object

When you call a Web service, you actually send a message to the service and then receive another message as a response. You send a message via the callService() method. Here is an example for calling a Web service:

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

The response message, or errors encountered during the interaction with the Web service, are attached to an object, called the result object. If you want to find the result of your query to the Web service, you need to analyze the result object. First, you need to know where you can get it for analysis. Well, it depends on how you call the Web service. If you specify an event handler in the callService() method (first parameter), the result object will be passed as a parameter to this event handler. Here is the handleResult() function from the above callService() example:

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

The result object includes everything you need in order to analyze what happened to the message you have sent to a Web service. It has the following properties:

Here is another example that calls the add method with two arguments, intA and intB:

iCallID = service.MyMath.callService(mathResults,"add",
  intA, intB);

The event handler mathResults() accepts the result object as its sole parameter:

function mathResults(result)

The name of the parameter can be different than "result". Inside, it checks whether there were errors during the call to the Web service:

if (result.error) { .... }

Use this check to write an event handler that processes the result object and prints the error details (if any) as well as the result value. Here is a Microsoft example:

<SCRIPT language="JavaScript">
<!--
// All these variables must be global,
// because they are used in both init() and onResult().
var iCallID = 0;
var intA = 5;
var intB = 6;
function init() {
  service.useService("/services/math.asmx?WSDL","MyMath");
  iCallID = service.MyMath.callService(mathResults,
    "add", intA, intB);
}
function mathResults(result) {
  if(result.error) {
    var xfaultcode   = result.errorDetail.code;
    var xfaultstring = result.errorDetail.string;
    var xfaultsoap   = result.errorDetail.raw;
  } else{
      alert(intA + ' + ' + intB + " = " + result.value);
    }
}
// -->
</SCRIPT>
<BODY onload="init()">
<DIV ID="service" style="behavior:url(webservice.htc)">
</DIV>
</HTML>

Next: How to handle the response through the event.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/5.html