Main Page

SOAP response

oBuffer.append(“xmlns:soapenc=\”http://schemas.xmlsoap.org/soap/encoding/\” “);
oBuffer.append(“xmlns:xs=\”http://www.w3.org/2001/XMLSchema\” “);
oBuffer.append(“xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”>”);
oBuffer.append(“<soap:Body soap:encodingStyle=”);
oBuffer.append(“\”http://schemas.xmlsoap.org/soap/encoding/\”>”);
oBuffer.append(“<n:getTemp><zipcode xsi:type=\”xs:string\”>”);
oBuffer.append(this.zipcode);
oBuffer.append(“</zipcode></n:getTemp></soap:Body></soap:Envelope>”);
return oBuffer.toString();
};
Because the SOAP request string is so long, this method uses the
StringBuffer()
object created earlier
in the book to build up the string. Note that the
zipcode
property is used here to insert the zip code in
question instead of passing it in as an argument. If the zip code is passed in as an argument, you must
rewrite the
send()
method to take this into account; this way, the
send()
method can be used as-is.
The
handleResponse()
method expects to receive a SOAP response string as its only argument. Once
again, this format can be determined by using the WSDL analyzer tool from XMethods:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<SOAP-ENV:Body>
<ns1:getTempResponse
SOAP-ENV:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”
xmlns:ns1=”urn:xmethods-Temperature”>
<return xsi:type=”xsd:float”><!--returned value--></return>
</ns1:getTempResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Despite all the extra code, the only part of interest is what is contained within the
<return/>
element.
The easiest way to extract that data from the string is to use a regular expression; there’s no need for
expensive DOM parsing and operations in this case (for more complicated Web services, however, that
may be an option). After the value is extracted, the
parseFloat()
function can be used to get the
floating-point value:
TemperatureService.prototype.handleResponse = function (sResponse) {
var oRE = /<return .*?>(.*)<\/return>/gi;
oRE.test(sResponse);
return parseFloat(RegExp[“$1”]);
};
The only thing left to do is modify the
send()
method to accept an argument (the zip code) because the
zipcode
property must be assigned for the method to function properly (remember, the
send()
method
calls
buildRequest()
, which uses the
zipcode
property to create the SOAP string).
To accomplish this, you first must create a pointer to the original
send()
method:
TemperatureService.prototype.webServiceSend = TemperatureService.prototype.send;
528
Chapter 17
20_579088 ch17.qxd 3/28/05 11:42 AM Page 528


JavaScript EditorFree JavaScript Editor     Ajax Editor


©