# University of Washington CSE 154

## Section 11: Web Services; JSON; JS Libraries

Except where otherwise noted, the contents of this document are Copyright © Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

# Exercise : Prime Factors XML (by Eli White)

Write a PHP web service factors-xml.php that computes and outputs prime factorization of integers as XML. (sample solution) (solution code) Provide the following behavior:

• A GET request to factors.php with an n parameter outputs the prime factors of that integer. For example, the request factors.php?n=264 would output:
<integer value="264">
<factor>2</factor>
<factor>2</factor>
<factor>2</factor>
<factor>3</factor>
<factor>11</factor>
</integer>

One algorithm for prime factorization of n is to loop through each integer i from 2-n, and for each time n is divisible by i, add i to your list of prime factors.

• A GET request without a proper n parameter should return an HTTP error code 400 (Invalid Request) with an instructive error message.

# Exercise : Prime Factors JSON (by Eli White)

Write a PHP web service factors-json.php that computes and outputs prime factorization of integers as JSON. (sample solution) (solution code) Provide the following behavior:

• A GET request to factors.php with an n parameter outputs the prime factors of that integer. For example, the request factors.php?n=264 would output:
{"value": 264, "factors": [2, 2, 2, 3, 11]}

One algorithm for prime factorization of n is to loop through each integer i from 2-n, and for each time n is divisible by i, add i to your list of prime factors.

• A GET request without a proper n parameter should return an HTTP error code 400 (Invalid Request) with an instructive error message.

# Exercise : Address Book, Client-Side (by Alex Miller)

Write the JS code to connect to a server-side JSON address book called addressbook.php. Start from this HTML. (sample solution) (solution)

• A GET request to addressbook.php with no parameters outputs an array of names:
{"names": ["Suzie", "Billy", ...]}
• A GET request with a name parameter outputs that person's address:
{"name": "Billy", "address": "123 4th St."}
• A POST request with a name and address parameter saves that person in the address book.

# Exercise : Address Book, Server-Side (by Alex Miller)

Write the PHP code to manage an address book using JSON. You are given the HTML and JS. The JS code sends requests to your addressbook.php web service that reads and saves address data. (sample solution) (solution) Write the PHP file, addressbook.php, that provides the following behavior:

• A GET request to addressbook.php with no parameters outputs an array of names:
{"names": ["Suzie", "Billy", ...]}
• A GET request with a name parameter outputs that person's address:
{"name": "Billy", "address": "123 4th St."}
• A POST request with a name and address parameter saves that person in the address book.

You may assume that any necessary parameters are passed and are in valid formats.

# Exercise : Garbage Collector (by Morgan Doocy)

Write the necessary JavaScript code to add effects and drag-and-drop behavior to a page. Start from this HTML/JS. (sample solution) (Prototype solution code) (jQuery solution code)

• When the page loads up, the IE icons should appear using an animated effect.
• When an IE icon is dragged and dropped onto the trash can, perform an animated effect and vanish it from the page.

Use Prototype/Scriptaculous or jQuery to add the effects and drag-and-drop behavior.