Calling a Web Service from a Mozilla Browser


var soapcall = new SOAPCall(); 

The Mozilla browsers support their own ways of calling Web Services. The JavaScript SOAPCall class can take care of creating the SOAP request. After instantiating the class, you have to set a couple of properties: the SOAP action (a SOAP header field, often the namespace of the Web Service plus the method name) and the absolute URI (including the name of the server) of the Web Service:

var soapcall = new SOAPCall(); soapcall.actionURI =   "http://javascript.phrasebook.org/randomNumber"; soapcall.transportURI =   "http://localhost/js/webservice.php"; 


Next, you create an array of all parameters you want to use in the Web Service call, by instantiating the SOAPParameter class:

var parameters = [   new SOAPParameter(1, "lower"),   new SOAPParameter(49, "upper") ]; 


Then, the encode() method prepares the Web Service call. The following parameters are expected:

  • SOAP version (0 stands for SOAP 1.1 and is recommended)

  • Name of the Web Service method to call

  • Namespace of the Web Service

  • Additional SOAP headers to send (as an array)

  • Number of parameters to send

  • Parameters to send (as an array)

The asyncInvoke() method actually calls the Web Service; you can provide a callback function that gets executed when the data comes back from the Web Service. This is an XML document, so you have to use the DOM to access the important information. The following code usually works:

function callbackFunction(result) {   var returnData = result.body.firstChild.firstChild.firstChild.data; } 


Here is the complete code:

Calling a Web Service from Mozilla (webservice-mozilla-php.html)

<html> <head>   <title>JavaScript</title>   <script language="Javascript"     type="text/javascript">   function callWebService(f) {     var soapcall = new SOAPCall();     soapcall.actionURI = "http://javascript.phrasebook.org/randomNumber";     soapcall.transportURI =       "http://localhost/js/webservice.php";     var parameters = [       new SOAPParameter(parseInt(         f.elements["lower"].value), "lower"),       new SOAPParameter(parseInt(         f.elements["upper"].value), "upper")     ];     soapcall.encode(       0,       "randomNumber",       "http://javascript.phrasebook.org/",       0,       [],       parameters.length,       parameters     );     soapcall.asyncInvoke(callbackFunction);   }   function callbackFunction(result) {     document.getElementById("random").innerHTML =       result.body.firstChild.firstChild.firstChild.data;   }   </script> </head> <body>   <form>     A number between     <input type="text" name="lower" size="3"       value="1" /> and     <input type="text" name="upper" size="3"       value="49" /> is     <span  /><br />     <input type="button" value="Retrieve"       onclick="callWebService(this.form);" />   </form> </body> </html> 

Note

Again, do note that you may have to change the URL for the Web Service call so that it fits your system.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net