Calling a Web Service from Internet Explorer


WebService.useService("webservice.php?WSDL",   "RandomNumberService"); 

Some years ago, Microsoft provided a helper script to access a Web Service from an HTML page using JavaScript. For this to work, the Web Service must reside on the same server as the JavaScript code.

Unfortunately, Microsoft stopped development of this component and hid it deep in the Microsoft Developers Network (MSDN). However, it is still available and it works fine. Go to http://msdn.microsoft.com/archive/en-us/samples/internet/behaviors/library/webservice/default.asp, download the file webservice.htc, and put the file in the directory where your HTML file will reside (the webservice.htc file is not part of the source code download for this book). Then, the following markup loads the component:

<div    style="behavior:url(webservice.htc);"> 


Then, WebService references the component and allows you to load a remote Web Service (useService() method), providing its WSDL description. The component then enables JavaScript code to call a web method (callService() method, providing a callback function, a method name, and parameters).

In the callback function, the result can be analyzed or just printed out, as the following sample code shows:

Calling Web Services from Internet Explorer (webservice-ie-php.html)

<html> <head>   <title>JavaScript</title>   <script language="Javascript"     type="text/javascript">   function callWebService(f) {     WebService.useService("webservice.php?WSDL",       "RandomNumberService");     WebService.RandomNumberService.callService(       callbackFunction,       "randomNumber",       parseInt(f.elements["lower"].value),       parseInt(f.elements["upper"].value));   }   function callbackFunction(result) {     document.getElementById("random").innerHTML =       result.value;   }   </script> </head> <body>   <div      style="behavior:url(webservice.htc);">   </div>   <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> 

Figure 10.2 shows the result: The random number comes from the Web Service on the web server.

Figure 10.2. The random number comes from the Web Service.


Note

The preceding listing covers calling the PHP web service; the file webservice-ie-aspnet.html deals with the ASP.NET Web Service. The only change is that the name of the Web Service's URL has been changed; the rest of the code remains the same.





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