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.
Todays lesson
In the last lesson we learnt that an HTTP Request is when we ask the server for some information.
In the two earlier 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,
data: { 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.correct
to check if the response was successful or not
- Use
-
Update the displayed word
-
Update the stored token
- Update remaining attempts and display all guesses
- Append each attempt to the
$('.attempts')
using a span - If the attempt is successful, include the class
correct
in the span; if it is unsuccessful, include the classwrong
- You can then find out how many wrong attempts there were using
$('.wrong').length+1;
- Append each attempt to the
- Issue PUT request
-
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 numbers, 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
error
to the letter field when the character is not allowed.
-
Other help
- Use
toLowerCase()
for comparing strings asa
is 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.
Extras
Now that you are familiar with HTTP requests, AJAX and APIs, how about you go away and create a webpage that pulls in all instagram pictures with a certain hashtag.
Or embed a google map onto a webpage with it pointing to a destination of your choice in London.