↑
Main Page
WebService component example
var oResult = window.event.result;
if (oResult.id == iCallID) {
if (oResult.error) {
alert(“An error occurred: “ + oResult.errorDetail.string);
} else {
alert(“Received back: “ + oResult.value);
}
}
};
iCallID = oService.FriendlyName.callService(...);
In this code, the
onresult
event handler first checks whether the result it’s handling is the response for
the appropriate request (in this way, a single
WebService
object can handle multiple requests). If the ID
of the result matches the call ID, the result is processed. The function ensures that there are no errors; if
an error occurs, the detailed error message is returned; otherwise the returned value is displayed.
Of course, it is up to you if you want to use the
value
property directly or use the
raw
property to parse
the returned SOAP code on your own.
WebService component example
This example uses the Temperature Service described in the sample WSDL earlier in the chapter. Because
the Microsoft
WebService
component requires you to know only the WSDL location and the name of the
operation you want to call, you don’t need the WSDL to get this working.
The Web page for this example consists of a text box (with the ID
“txtZip”
) and a button (labeled
“Get
Temperature”
). The user enters a zip code into the text box and then clicks the button to get the temper-
ature in that zip code (he calls the Web service). Of course, you also need an element to which you can
assign the
WebService
component. Here is the HTML:
<html>
<head>
<title>IE Web Service Example</title>
<script type=”text/javascript”>
//...
</script>
</head>
<body>
<p><input type=”text” id=”txtZip” size=”10” /><input type=”button”
value=”Get Temperature” onclick=”callWebService()” />
<div id=”service” style=”behavior:url(webservice.htc)”
onresult=”onWebServiceResult()”></div>
</body>
</html>
The JavaScript to run this page is fairly simple:
var iCallID = null;
var sWSDL = “http://www.xmethods.net/sd/2001/TemperatureService.wsdl”;
515
Web Services
20_579088 ch17.qxd 3/28/05 11:42 AM Page 515
Free JavaScript Editor
Ajax Editor
©
→