For years there has been a software engineering problem: invoking services or methods on another machine from one machine, even if the machines use completely different hardware or software. The most recently proposed solution for this problem is Web services. A few years ago, Web services were touted as a dazzling halo around the head, and some thought Web services were the "Holy Grail" of distributed software development. Later, its light gradually dimmed, the Web service finally found its own suitable location, it is to support the interoperability of heterogeneous computer systems a useful tool.
Web services are often used as communication pipelines between computer systems, similar to CORBA (Common Object Request Broker architecture), RMI (remote method invocation), or DCOM (Distributed Component Object Model). The difference is that Web services are independent of specific developers and can be implemented using a large number of programming tools and platforms. To support higher levels of interoperability, Web services are text-based protocols that are typically implemented on top of HTTP. Because Web services are text-based protocols, some kind of XML is almost always used.
The most famous Web service implementation is SOAP (Simple Object Access Protocol). Soap is a protocol that is managed by the consortium, which is XML and defines how to invoke a remote procedure.
The WSDL (Web Service Description Language) document is also an XML document that describes how to create a customer for a Web service. By providing a WSDL document, a Web service provider can easily create client code for a potential customer. WSDL and SOAP are commonly used together, but this is not necessarily the case because the two statutes are maintained separately.
Although much effort has been made to simplify the implementation of soap, soap is a very difficult technology to use and is therefore "marginalized" and uses soap only if cross-platform interoperability is indeed an important requirement. There is also a simpler way to implement Web services, called rest (representing state transfers), which is gaining visibility among developers, who want 80% of the benefits of soap and only 20% of the soap price.
Yahoo! chooses rest as the protocol for its public Web services. Yahoo! that rest-based services are easy to understand, and that rest is "approachable" because most programming languages currently have access to rest. In fact, Yahoo! believes that the threshold for rest is lower and easier to use than soap.
By using rest, you can create a request by specifying a service entry URL and then appending the search parameters to the query string. The service returns the result as an XML document. Does this pattern sound familiar? You're right, it's the same as the Ajax examples you've seen in this book.
The XMLHttpRequest object is ideal for customers who are based on restful Web services. With the XMLHttpRequest object, you can asynchronously request a Web service and parse the resulting XML response. For Yahoo! Web service, the XMLHttpRequest object can send a request to the Yahoo! to search for the specified item. Once Yahoo! returns the response, the JavaScript Dom method is used to parse the response and provide the resulting data dynamically to the page.
Code Listing 4-15 shows how to use AJAX technology to access Yahoo! Web service and provide results to the page. The text field on the page allows the user to specify the search term. The user can use a selection box to specify how many results to display. Click on the Submit button to start the search.
But, wait! In the 2nd Chapter we have said that the XMLHttpRequest object can only access resources in the domain where the originating document (that is, the calling script) resides. If you try to access resources from another domain, you may fail because of security restrictions on your browser. How to solve it?
There are several solutions. The 2nd chapter has learned that browsers implement a safe sandbox in different ways. IE asks the user whether to allow access to resources in another domain. Firefox will report errors and fail automatically, although this behavior can be avoided with JavaScript code dedicated to Firefox.
There is also a choice, which is also the method to be used in this example, is to establish a Yahoo! gateway, which is associated with the xmlhttp-
The request script is in the same domain. The gateway receives the request from the XMLHttpRequest object and forwards it to the Yahoo! Web Services. The gateway then routes the results to the browser when the Yahoo! response returns the result. By using this method, you can avoid using browser-specific JavaScript. In addition, this approach is more robust because you can also extend the gateway to support other Web service providers.