Skip to content

Commit e1bc346

Browse files
committed
JS Lesson 4 update
The GitHub API changed and removed Gravatar URLs completely: https://developer.github.com/changes/2014-09-05-removing-gravatar-id/ My students also had some trouble following the instructions given the very loose structure. I added a bit more explanation around JSON and guidance for implementing the XHR call method. A student also mentioned that is what hard to follow because they didn't know what the end goal of the tutorial is, so I added a link to the finished example to the top.
1 parent f5d5e2a commit e1bc346

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

js/lesson4/tutorial.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ title: HTTP Requests, AJAX and APIs
66
In the last lesson we've introduced [jQuery](http://jquery.com/download/).
77
Today, we will be explaining HTTP Requests, using AJAX and APIs.
88

9+
Our goal is to implement a [Github User Finder](../../examples/github-user-finder/index.html)
10+
that allows us to look up GitHub users by the user name.
11+
912
## Before we start...
1013

1114
Don't forget to move the files for the exercises under your Github page folder. Commit each task you complete! If you are having trouble with this, ask your coach to help you out.
1215

13-
#HTTP Requests
16+
# HTTP Requests
1417

15-
##What are HTTP Requests?
18+
## What are HTTP Requests?
1619

17-
Every time the browser fetches data from a server (which could be a page, an images, a script etc) it does it using HTTP. HTTP is the **H**&#x200b;yper<strong>T</strong>ext **T**&#x200b;ransport **P**&#x200b;rotocol. The server then gives back a **response**
20+
Every time the browser fetches data from a server (which could be a page, an images, a script etc) it does it using HTTP. HTTP is the **H**&#x200b;yper<strong>T</strong>ext **T**&#x200b;ransport **P**&#x200b;rotocol. The server then sends back a **response**
1821

1922
Here is an example of the **GET** requests issued by the [wishlist tutorial](http://codebar.github.io/tutorials/examples/wishlist/index.html).
2023

21-
*You can view any requests issued by a website by going to the Network (or Net) tab.
24+
\*You can view any requests issued by a website by going to the Network (or Net) tab.
2225

2326
![](assets/images/inspector_requests.png)
2427

@@ -55,25 +58,29 @@ The `open()` method specified the type of request, the URL and if the request ca
5558

5659
`xmlhttp.open(<request verb>, url, <async:true|false>);`
5760

58-
The different between using a synchronous and an asynchronous request, is that our page will wait until the request has been completed, if we specify **synchronous**, so you should almost always use **asynchronous**
61+
The different between using a synchronous and an asynchronous request is that our page will wait until the request has been completed, if we specify **synchronous**. That means that no interaction with the page is possible while that request is waiting to be completed. So you should always use **asynchronous**.
5962

6063
**An example using an asynchronous request**
6164

6265
```javascript
63-
xmlhttp = new XMLHttpRequest();
64-
xmlhttp.open("GET", url, true);
65-
xmlhttp.send();
66-
return xmlhttp;
66+
var xmlhttp = new XMLHttpRequest();
67+
xmlhttp.open("GET", "https://api.github.com/", true);
68+
xmlhttp.send();
6769
```
6870

69-
To retrieve the response, we need to access `xmlhttp.responseText`. Before doing that though, we should make sure that the `xmlhttp.status` is `200`, as there will not be a response for failing requests.
71+
To retrieve the response, we need to access `xmlhttp.responseText`. Before doing that though, we should make sure that the `xmlhttp.status` is `200`. Otherwise we might accidentally try to work with an error response.
7072

7173
> Have another look at the response status codes we mentioned earlier. What is `200`?
7274
73-
To be able to utilise the response we get, we need to convert it to `JSON`.
75+
To be able to utilise the response we get, we need to convert it to `JSON`. JSON
76+
is a way of turning JavaScript objects into strings and vice versa. This is very
77+
useful if we want to exchange data over HTTP which works understands with text.
78+
You can use `JSON.parse` to turn JSON strings into real JavaScript objects.
7479

7580
```js
76-
JSON.parse(response)
81+
var londonJson = '"{"name":"London","population":8308369}"';
82+
var london = JSON.parse(response);
83+
console.log(london.name);
7784
```
7885

7986
> JSON stands for &#x200b;**J**&#x200b;ava&#x200b;**S**&#x200b;cript &#x200b;**O**&#x200b;bject &#x200b;**N**&#x200b;otation.
@@ -90,7 +97,8 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
9097
{
9198
"login": "octocat",
9299
"id": 1,
93-
"gravatar_id": "somehexcode",
100+
"avatar_url": "https://avatars.githubusercontent.com/u/9906?v=2",
101+
"gravatar_id": "",
94102
"html_url": "https://github.com/octocat",
95103
"type": "User",
96104
"name": "monalisa octocat",
@@ -104,7 +112,7 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
104112

105113
```
106114

107-
*You can read more information about the request structure in the [Github API](http://developer.github.com/v3/users/#response). This is not necessary for this exercise.
115+
\*You can read more information about the request structure in the [Github API](http://developer.github.com/v3/users/#response). This is not necessary for this exercise.
108116

109117
> After parsing the response, you can access the data using the **dot** notation. For example, if we've parse the response into a user variable `var user = JSON.parse(responseText)`, we can access `user.login`
110118
@@ -113,8 +121,11 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
113121
First, let's create a function that does the AJAX call to the GitHub API.
114122

115123
```js
116-
function getGithubInfo(user) {
117-
//call to API
124+
function getGithubInfo(username) {
125+
var xhr = new XMLHttpRequest();
126+
xhr.open(...);
127+
128+
return xhr;
118129
}
119130
```
120131

@@ -125,11 +136,10 @@ To test this out, let's handle the keypress on the input field. We want this to
125136
```js
126137
$(document).ready(function(){
127138
$(document).on('keypress', '#username', function(e){
128-
if (e.which == 13) {
139+
if (e.which === 13) {
129140
// get val() from input field
130141

131-
// assign getGithubUserInfo(username) to a variable response
132-
142+
// assign getGithubUserInfo(username) to a variable response
133143
}
134144
})
135145
});
@@ -161,9 +171,8 @@ The `showUser(user)` function should:
161171

162172
2. Add a link to the user's Github profile in `#profile .information`. The link should have a class `profile`
163173

164-
3. Add an image in `#profile .avatar`. To do that, you can use the `gravatar_id` in this URL template `<img src="https://gravatar.com/avatar/<gravatar_id>?s=220" />`
165-
166-
*You can read more about [Gravatar Image Requests](http://uk.gravatar.com/site/implement/images/).
174+
3. Add an image in `#profile .avatar`. To do that, you can use the `avatar_url`
175+
from the response.
167176

168177
> Don't forget to call `showUser()` from the function handling the keypress!
169178

0 commit comments

Comments
 (0)