↑
Main Page
Web service support
function WebService() {
this.action = “”;
this.url = “”;
}
WebService.prototype.buildRequest = function () {
return “”;
};
WebService.prototype.handleResponse = function (sSOAP) {
};
WebService.prototype.send = function () {
if (isMoz) {
try {
netscape.security.PrivilegeManager.enablePrivilege(“UniversalBrowserRead”);
} catch (oError) {
alert(oError);
return false;
}
}
var oRequest = new XMLHttpRequest;
oRequest.open(“post”, this.url, false);
oRequest.setRequestHeader(“Content-Type”, “text/xml”);
oRequest.setRequestHeader(“SOAPAction”, this.action);
oRequest.send(this.buildRequest());
if (oRequest.status == 200) {
return this.handleResponse(oRequest.responseText);
} else {
throw new Error(“Request did not complete, code “ + oRequest.status);
}
};
The
WebService
object is the basis of cross-browser Web service support. It has two properties: the URL
for the SOAP request (
url
) and the SOAP action (
action
). Both properties are initialized to
null
; sub-
classes fill these in as needed. The
buildRequest()
method is intended to build the SOAP message
string; but in this case, it just returns an empty string. The
handleResponse()
method receives the
SOAP message from the response and returns the appropriate value.
The
send()
method does the heavy lifting for this functionality. First, if the browser is Mozilla, you
must request the
UniversalBrowserRead
privilege. Then, an
XMLHttpRequest
object is created
(remember, this code uses the wrapper for IE), and the SOAP request is built up. This request includes
opening a request to the specified URL, setting the content type to
“text/xml”
and setting the SOAP
action. Then, the
buildRequest()
method is called to get the SOAP message and send it using the
XMLHttpRequest
’s
send()
method.
Lastly, if the request returns a status of 200, the text returned is interpreted by the
handleResponse()
and that value is returned. Otherwise, an error is thrown. The intent is to allow you to use a
WebService
object like this:
526
Chapter 17
20_579088 ch17.qxd 3/28/05 11:42 AM Page 526
Free JavaScript Editor
Ajax Editor
©
→