18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Web</strong> Services<br />

The pertinent information (highlighted previously) from this file is the following:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

The only operation is called getTemp.<br />

The SOAP action for getTemp is “” (an empty string).<br />

The target namespace for the input to getTemp is urn:xmethods-Temperature.<br />

The name of the port is TemperaturePort.<br />

The service location is http://services.xmethods.net:80/soap/servlet/rpcrouter.<br />

Depending on the type of consumer used to access <strong>Web</strong> services, you may need to know some or all the<br />

previous information.<br />

<strong>Web</strong> Services in Internet Explorer<br />

The folks over at Microsoft were kind enough to create an HTML component (also called a behavior)<br />

that hides a lot of the ugly details from developers who wish to consume <strong>Web</strong> services. An HTML component<br />

is essentially a COM component defined using XML and <strong>JavaScript</strong>. HTML components can<br />

have properties, methods, and support custom events, making them ideal for creating functionality that<br />

doesn’t exist in the browser by default. The downside is that only Internet Explorer supports HTML<br />

components, and thus Microsoft’s <strong>Web</strong>Service component does not work in other browsers. The<br />

<strong>Web</strong>Service component is available for free from Microsoft’s <strong>Web</strong> site (http://msdn.microsoft.com/<br />

library/default.asp?url=/workshop/author/webservice/webservice.asp).<br />

Using the <strong>Web</strong>Service component<br />

After the webservice.htc file is downloaded, place it in the directory with your <strong>JavaScript</strong> files. You<br />

can then access the functionality by applying it to an HTML element. To do this, use the style attribute<br />

and the custom behavior CSS attribute:<br />

<br />

After doing this, the HTML element takes on all the properties, methods, and events of the <strong>Web</strong>Service<br />

component. To use the component as a <strong>JavaScript</strong> object, just assign an ID to the element:<br />

<br />

Then use the document.getElementById() method to retrieve a reference:<br />

var oService = document.getElementById(“service”);<br />

Next, you need to specify a <strong>Web</strong> service to use by calling useService(). The useService() method<br />

accepts two parameters: the WSDL file describing the service and a friendly name for the service. A typical<br />

call looks like this:<br />

oService.useService(sUrl, “FriendlyName”);<br />

513

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!