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.
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.open("GET", "url", true);
ajax.send();
...
function functionName() {
if (this.status == 200) { // request succeeded
do something with this.responseText
;
} else {
code to handle the error;
}
}
Create an AJAX-powered gallery of pet images that allows you to switch between kitty and puppy images without reloading the page. You can view the finished product here.
You are provided with the following HTML file, ajaxpets.html:
You must write a JavaScript file ajaxpets.js
which requests data
from the ajaxpets.php
script on webster with the parameter animal of value kitty or puppy, depending on which radio button is selected, and injects this into the #pictures div.
window.onload = function() { document.getElementById("puppies").onclick = updatePictures; document.getElementById("kitties").onclick = updatePictures; }; function updatePictures() { var animal = ""; if (document.getElementById("puppies").checked) { animal = "puppy"; } else { animal = "kitty"; } var url = "//webster.cs.washington.edu/cse154/sections/ajax/pets/ajaxpets.php?animal=" + animal; var ajax = new XMLHttpRequest(); ajax.onload = displayPictures; ajax.open("GET", url, true); ajax.send(); } function displayPictures() { document.getElementById("pictures").innerHTML = this.responseText; }
Write a "Boot Loader" page that displays a randomly chosen fashionable boot when the "Load Boot" button is clicked. Start from this HTML skeleton. (sample solution)
loader.php
on Webster when the 'Load boot' button is clicked.
img
tag into the page that points to that image.
id
of loading
that can be used to show an animated image while the page is waiting. Set its display
DOM CSS property appropriately.
alert
on errors so the user knows something went wrong.
Don't try to display the boot image when there is an error.
You can view the solution javascript file here.
Given this HTML skeleton, write the necessary JavaScript code to make the page into a chat program.
Read and submit chat messages by making Ajax requests to the provided
(chatit.php
) on Webster.
(sample solution)
GET
request to ask the PHP script for all messages.
Optional parameters:
reverse
returns output in reverse-chronological order;
limit
returns only that many most recent posts
POST
request to the PHP script with the parameter msg
to submit a message.
(see next slide and/or Ajax POST slides)
POST
requestvar params = new FormData(); params.append("name", value); params.append("name", value); var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("POST", "url", true); ajax.send(params);
FormData
object to gather your POST query parametersFormData
to the request's send
methodmethod
passed to open
should be changed to "POST"
You can view the solution JavaScript code here.
Given this HTML file,
write an accompanying recipe.js that displays recipe information on
the page with ajax. When a radio button is clicked make an ajax GET
request to
recipes.php
with a parameter r
that has the value of the radio button.
The results from recipes.php
is in xml format. You must parse the
results and fill the #main
div with with a h1 containing the title,
an img tag with the recipe's image and a ul filled with the recipe's ingredients.
Check out a working example here.
The result xml from recipes.php has the following structure (and some extra stuff):
<recipe> <information> <name></name> <image></image> </information> <ingredients> <item amount=""></item> ... </ingredients> <directions> <step></step> ... </directions> </recipe>
You can view the solution javascript here.
Today we will write a page "Pet-It" that shows information about breeds of cats and dogs using Ajax. The HTML and CSS are already written; start from them:
Add the JavaScript event-handling and Ajax code. Click the following image to run
our sample solution:
(solution JS code petit.js
)
All required files are on Webster
http://webster.cs.washington.edu/cse154/sections/ajax/
,
or you can download them as pet-it.zip
.
Breeds are located in the subdirectory breeds/
relative to the previous URL.
dogs.txt
and cats.txt
.
breeds/chow_chow.html
and
breeds/chow_chow.jpg
.
selected
). If the user reaches the last breed and clicks Next, wrap around to the start.
You can view the solution javascript here.
Write the JS code to connect to a server-side JSON address book called
addressbook.php
.
Start from this HTML.
(sample solution)
(solution)
addressbook.php
with no parameters outputs an array of names:
{"names": ["Suzie", "Billy", ...]}
name
parameter outputs that person's address:
{"name": "Billy", "address": "123 4th St."}
name
and address
parameter saves that person in the address book.
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:
addressbook.php
with no parameters outputs an array of names:
{"names": ["Suzie", "Billy", ...]}
name
parameter outputs that person's address:
{"name": "Billy", "address": "123 4th St."}
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.
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:
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.
n
parameter should return an HTTP error code 400 (Invalid Request) with an instructive error message.
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:
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.
n
parameter should return an HTTP error code 400 (Invalid Request) with an instructive error message.