↑
Main Page
SOAP error
Once again, the first function is named
callWebService()
. The first step must be a request for
enhanced privileges as described previously. If the privileges aren’t given, there’s no point in the func-
tion continuing, so it returns. If the privileges are enabled, then the zip code is retrieved and placed in a
SOAPParameter
array. Lastly, the SOAP call is created and made asynchronously:
function callWebService() {
try {
netscape.security.PrivilegeManager.enablePrivilege(“UniversalBrowserRead”);
} catch (e) {
alert(e);
return false;
}
var sZip = document.getElementById(“txtZip”).value;
var arrParams = new Array;
arrParams[0] = new SOAPParameter(sZip, “zipcode”);
var oSoapCall = new SOAPCall();
oSoapCall.transportURI = sURL;
oSoapCall.encode(0, “getTemp”, sTargetNamespace, 0, null,
arrParams.length, arrParams);
oSoapCall.asyncInvoke(onWebServiceResult);
}
Next, the
onWebServiceResult()
function handles the response. Most of the function is dedicated to
determining if an error occurred during the Web service call:
function onWebServiceResult(oResponse,oSoapCall,iError) {
if (iError != 0) {
alert(“An unspecified error occurred.”);
} else if (oResponse.fault != null) {
alert(“An error occurred (code=” + oResponse.fault.faultCode
+ “, string=” + oResponse.fault.faultString + “)”);
} else {
var oParams = oResponse.getParameters(false, {});
alert(“It is currently “ + oParams[0].element.firstChild.nodeValue
+ “ degrees in that zip code.”);
}
}
The first step in this function is to check the error code
iError
. If it’s not equal to
0
, that means there
isn’t a response to evaluate, so an error message is displayed. If the error code is
0
, the next step is to
determine that there wasn’t a SOAP error. This ensures that the
fault
property is
null
(if it’s not, a
detailed error message is displayed).
When the two other conditions fail, the response must be valid. So, the value of the first parameter is
displayed in a user-friendly message.
521
Web Services
20_579088 ch17.qxd 3/28/05 11:42 AM Page 521
Free JavaScript Editor
Ajax Editor
©
→