Automate data entry with Web services and Ajax
The list below shows the steps involved in completing this solution. (Don’t worry; the remainder of
this article walks you through these steps one by one.) Note that a partial is a Ruby on Rails term.
It’s a reusable piece of code related to what gets displayed in the Web browser. Most modern
frameworks include some kind of template and partial functionality that dynamically assembles
pieces of the template to produce a Web page. Partials are a real convenience to application developers
and rastically help reduce the burden of development. The RoR naming convention is to prefix a
partial with an underscore (for example, _addressForm.rhtml).
- Modify the _form.rhtml partial to display the ZIP code before the city and state.
- Add a partial (_cityState.rhtml) to display the input fields for city and state.
- 3. Modify the _form.rhtml partial to “listen” for changes to the ZIP code field and make an
Ajax call to the server.
- Modify the controller to validate the ZIP code (5 numeric digits). If not valid, return a blank
Ajax response to the client.
- Modify the controller to create a valid XML request to send to the USPS Web service.
- Modify the controller to receive and parse a XML response from the USPS Web service.
- Modify the Ajax response to populate the _cityState partial with the Web services’ values.
- Figure out some ways to improve the solution, and e-mail the author with your suggestions.
June 2008 | Java Jazz Up | 15