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() {
do something with this.responseText or this.responseXML;
}
ajax.responseText contains the data in plain text (a string)ajax.responseXML is a parsed XML DOM tree object
var elms = node.getElementsByTagName("tag"); // get a group of nodes
var text = node.firstChild.nodeValue; // get text inside a node
var attrValue = node.getAttribute("name"); // get an attribute's value
Given this HTML file, write an accompanying recipe.js that displays recipe information on the page with Ajax.
(sample solution)
GET request to recipes.php with a parameter r that has the value of the radio button, such as peas.
recipes.php are in XML format.
#main div with a h1 containing the titleimg tag with the recipe's imageul filled with the recipe's ingredients
The result XML from recipes.php has the following structure (and some extra stuff):
<recipe> <information> <name>Peas with Butter</name> <image>https://webster.cs.washington.edu/cse154/sections/ajax/recipe/peas.jpg</image> <author>Vance McElmurry</author> <category>Entree</category> <description serves="4">A simple, but delicious frozen pea recipe.</description> </information> <ingredients> <item amount="1 lb">Frozen Peas</item> <item amount="1/4 cup">Water</item> <item amount="to taste">Salt</item> </ingredients> <directions> <step>Place the frozen peas and water in a microwavable bowl.</step> <step>Place the lid on th ebowl and cook in th emicorwave for 4 minutes.</step> </directions> </recipe>
You can view the solution javascript here.
If you finish the basic exercise, try adding more of the recipe info to the page, such as:
Given this HTML/JS skeleton, add the necessary additional JavaScript code to use Ajax requests to save and load the student's grades from the server.
This is similar to the previous version of this problem that used localStorage, but saving the grades on the server means the student can see them from any computer.
(sample solution)
(see next slides)
Make it so that when the Look Up button is clicked, any saved scores on the server for the current user ID are loaded into the text boxes on the page.
Make an Ajax GET request to grades.php with a parameter uwnetid.
Test your code with user ID test for now.
The result XML from grades.php has the following structure:
<scores> <score item="hw1">10</score> <score item="hw4">22</score> <score item="hw5">9</score> <score item="hw7">17</score> <score item="final">58</score> </scores>
The server will send back a plain text response to your POST request letting you know what happened.
Inject this text into the page in the area with the id of output.
Make it so that when the Save button is clicked, any non-blank scores typed into the page are saved on the server.
Make an Ajax POST request to grades.php with parameter names/values such as:
| name | value |
|---|---|
uwnetid |
jsmith |
hw1 |
15 |
hw3 |
23 |
final |
87 |
(see next slide for syntax of making an Ajax POST request)
POST request
var 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.
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. Add the JavaScript event-handling and Ajax code. (sample solution)
All required files are on Webster
(see directory).
Breeds are located in the subdirectory breeds/ .
dogs.xml and
cats.xml.
The XML data follows a format such as the following:
<breeds> <breed>Alaskan Malamute</breed> <breed>Beagle</breed> ... </breeds>
breeds/chow_chow.html and
breeds/chow_chow.jpg.
selected).You can view the solution javascript here.
If you finish the basic exercise, try adding more features to the page, such as: