Web Services, Part IV: WebService Behavior's Objects, Properties, and Events: Using the result Object - Doc JavaScript | WebReference

Web Services, Part IV: WebService Behavior's Objects, Properties, and Events: Using the result Object - Doc JavaScript


Web Services, Part IV: WebService Behavior's Objects, Properties, and Events

Using the result Object

The result object can be handled in two ways. If you specify a callback function in callService(), result is passed as the first parameter to this callback function. Here is an example:

<SCRIPT LANGUAGE="JavaScript">
var iCallID;
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>
</BODY>

If a callback function is not used, the event object of the onresult event exposes the result object. Here is an 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("add", intA, intB);
}
function onWSresult() {
  if((event.result.error)&&(iCallID==event.result.id)) {
    var xfaultcode   = event.result.errorDetail.code;
    var xfaultstring = event.result.errorDetail.string;
    var xfaultsoap   = event.result.errorDetail.raw;
  } else if((!event.result.error) && (iCallID ==
    event.result.id)) {
           alert(intA + ' + ' + intB + ' = '
             + event.result.value);
         } else {
             alert("Something else fired the event!");
           }
}
// -->
</SCRIPT>
<BODY onload="init()">
<DIV id="service" STYLE="behavior:url(webservice.htc)"
  onresult="onWSresult()">
</DIV>
</BODY>

Next: How to use the onserviceavailable event handler

https://www.internet.com


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

URL: https://www.webreference.com/js/column99/5.html