| layout | page |
|---|---|
| title | HTTP Requests, AJAX and APIs (part 2) |
This is the second tutorial on HTTP Requests, AJAX and APIs. You can find the first part of the tutorial.
In the last lesson we've explained an HTTP Requests is when we ask the server some information.
In the two exercises we used the GET request. Today we will be building a Hangman game using an existing API that will handle the game logic for us.
We will be using the POST, PUT and GET requests, and other things we've learned in the last couple of lessons.
| Verb | Description |
|---|---|
| GET | fetching a resource (e.g. /index.html will return the HTML of the page) |
| PUT | updating an existing resource. |
| POST | Create a new resource. |
##Request using JQuery
To use POST and PUT requests we must specify the type in the ajax() call that we introduced in the previous lesson.
You can also specify any data as a JSON object.
$.ajax({
type: request_type,
date: { field: 'value', other_field: 'other value' }
...
});##Exercise - Hangman!
Download the exercise files or clone them directly from Github git clone https://gist.github.com/c76a7bd0bef66713a9ac.git
###API
| type | resource | parameters | description |
|---|---|---|---|
| POST | http://hangman-api.herokuapp.com/hangman |
- | Create a new game |
| PUT | http://hangman-api.herokuapp.com/hangman |
{ token: game token, letter: guess } |
Guess a letter |
| GET | http://hangman-api.herokuapp.com/hangman |
{ token: game token } |
Get solution |
###What we will be doing:
-
Create a new game
-
Issue POST request
-
Update the displayed string on the page and store the token
- Use the hidden field with the class
token
- Use the hidden field with the class
-
Don't allow the user to start a new game, hide the New game bubtton
-
-
Interact with the API to try out different guesses
-
Issue PUT request
- Use
data.correctto check if the response was successful or not
- Use
-
Update the displayed word
-
Update the stored token
-
Update remaining attempts and display all guesses
- If an attempt is not succesful, appent it to the
$('.attempts')using a span with the classwrong - You can then find out how many wrong attempts there wer using
$('.wrong').length+1;
- If an attempt is not succesful, appent it to the
-
-
On the 7th failure, retrieve the solution using the GET request
- Display the solution, hide the input field and allow a user to start a new game
-
Bonus don't process letters, guesses that have already been attempted or empty space
-
You can use JQuery's
$.isNumeric(character))to check if a letter is a number -
trim()removes all space around a string. You can applytrim()and check for the length to make sure the guess is one character long -
All the attempted guesses are already in
.attempts. You can useindexOf(character)to check if it's contained in a string. -
Add the class
errorto the letter field when the character is not allowed.
-
- Use
toLowerCase()for comparing strings asais not the same asA
Here is our version of Hangman.
This ends our HTTP Requests, AJAX and APIs tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial send us an email and let us know.
