↑
Main Page
getTempCallback
var oProxyCreateCallback = {
onLoad: function(oCreatedProxy) {
oProxy = oCreatedProxy;
oProxy.setListener(oGetTempCallback);
},
onError: function(sError) {
alert(sError);
}
};
The callback object for operations is called
oGetTempCallback
, and looks like this:
var oGetTempCallback = {
getTempCallback: function (iDegrees) {
alert(“It is currently “ + iDegrees + “ degrees in that zip code.”);
}
}
As you can see, the only method is
getTempCallback()
. Because the Web service simply returns a
number, the first (and only) argument contains a simple number. The argument is named
iDegrees
,
but, in practice, it doesn’t matter what you name the argument. This method is called as soon as
getTemp
returns a value and displays the temperature in a user-friendly message.
Next, a function is needed to create the WSDL proxy. In many cases, it’s best to create the proxy during
the page’s
onload
event handler because of the asynchronous nature of proxy creation, so this example
assigns the function directly to
window.onload
:
function createProxy() {
try {
var oFactory = new WebServiceProxyFactory();
oFactory.createProxyAsync(sWSDL, sPort, “”, true, oProxyCreateCallback);
} catch (oError) {
alert(oError.message);
}
}
window.onload = createProxy;
This function creates a
WebServiceProxyFactory
and uses it to create a proxy, assigning the location
of the WSDL file, the port name, and the callback object. It also specifies all methods to be called
asynchronously.
Last is the
callWebService()
function. The function first checks that the proxy has been created before
attempting to make the call. If the proxy has been created, then
getTemp()
is called as a method of the
proxy and the zip code is passed in:
function callWebService() {
if (oProxy) {
var sZip = document.getElementById(“txtZip”).value;
oProxy.getTemp(sZip);
524
Chapter 17
20_579088 ch17.qxd 3/28/05 11:42 AM Page 524
Free JavaScript Editor
Ajax Editor
©
→