Web Services, Part III: WebService's Methods
Using the WebService Behavior
To invoke a method on a Web service, you first attach the
WebService behavior (webservice.htc) to any element in the page. You can use the
BODY element like this:
or you can insert a special
<BODY ID="webServiceCallerBody" STYLE="behavior:url(webservice.htc)">
DIVelement like here:
Once the behavior is attached, the
<DIV ID="webServiceCallerBody" STYLE="behavior:url(webservice.htc)"></DIV>
WebServicebehavior enables you to call Web services via the
callService()method. Here is an example:
iCallID = myService.MyMath.callService("add", int1, int2);
You can call Web services either synchronously or asynchronously. When you call a Web service asynchronously, there is a delay between the execution of the method and the arrival of its returned result. When you call a Web service synchronously, your script halts until the
callService() method has completed. The asynchronous mode of method invocation is the default mode of the
WebService behavior. Synchronous calls to remote services lock the user interface while the call is pending. Therefore, you should use asynchronous calls in browser-based applications.
WebService behavior handles the process of assembling a SOAP message and sending it to the Web service, and then receiving the raw XML data packets from the Web service. As we explained in Column 97 (Web Services, Part II: Calling Service Methods), you have the option of using either an event handler or a callback handler function to process the results. If you use an event handler, the
WebService fires the
onresult event, which occurs when the
WebService receives the response from the Web service. If you use a callback function, a
result object is passed directly as an input parameter to the callback function.
When you use the
WebService behavior, you should always test the
error property, to determine if the method invocation was successful. When an error is encountered, the
errorDetail object is exposed. Use its properties to help identify the source of the error.
You cannot use the
WebService behavior to directly invoke a method on a Web service that is hosted in a different domain from the machine hosting the Web page. To configure a page that uses the
WebService behavior to call methods from remote Web services, the Web server delivering pages to the client must act as a proxy for Web services running elsewhere. Until we teach you how to do this, follow our instructions in Column 97, and call Web services from your client machine.
Next: How to use the createUseOptions() method