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

Handling the event.result Object

If you don't specify an event handler in the callService() method, you can get the result object as a property of the event object. You first specify the onresult event handler like here:

<BODY ID="webServiceCallerBody" onresult="onWSresult()"
  style="behavior:url(webservice.htc)">

The event handler onWSresult() first checks to see if there was an error during the Web service call:

if (event.result.error)

Notice that the result object is a property of the event object. The event handler also checks that the event ID, as it came from the Web service, is identical to the ID returned by the callService() function:

if (iCallID==event.result.id)

With these two checks you can write an event handler that processes the result object, checks for errors and matching IDs, and prints the result value.

When checking for errors, the errorDetail object is quite handy. It is a property of the result object, and it includes detailed information about the Web service transaction. Here are its properties:

  • code. A machine-readable error code that corresponds to a specific invocation of the callService() method.
  • raw. This property exposes the raw Simple Object Access Protocol (SOAP) data packet returned by the Web Service after invoking the callService() method.
  • string. A human-readable error message that corresponds to a specific invocation of the callService() method.

The onWSresult() function in the following example reads the above parameters of event.result and prints them in an easier to understand fashion:

<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 define the calling buttons

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/6.html