You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: js/lesson4/tutorial.md
+31-22Lines changed: 31 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,22 @@ title: HTTP Requests, AJAX and APIs
6
6
In the last lesson we've introduced [jQuery](http://jquery.com/download/).
7
7
Today, we will be explaining HTTP Requests, using AJAX and APIs.
8
8
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
+
9
12
## Before we start...
10
13
11
14
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.
12
15
13
-
#HTTP Requests
16
+
#HTTP Requests
14
17
15
-
##What are HTTP Requests?
18
+
##What are HTTP Requests?
16
19
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**​yper<strong>T</strong>ext **T**​ransport **P**​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**​yper<strong>T</strong>ext **T**​ransport **P**​rotocol. The server then sends back a **response**
18
21
19
22
Here is an example of the **GET** requests issued by the [wishlist tutorial](http://codebar.github.io/tutorials/examples/wishlist/index.html).
20
23
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.
22
25
23
26

24
27
@@ -55,25 +58,29 @@ The `open()` method specified the type of request, the URL and if the request ca
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**.
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.
70
72
71
73
> Have another look at the response status codes we mentioned earlier. What is `200`?
72
74
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.
74
79
75
80
```js
76
-
JSON.parse(response)
81
+
var londonJson ='"{"name":"London","population":8308369}"';
82
+
var london =JSON.parse(response);
83
+
console.log(london.name);
77
84
```
78
85
79
86
> JSON stands for ​**J**​ava​**S**​cript ​**O**​bject ​**N**​otation.
@@ -90,7 +97,8 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
@@ -104,7 +112,7 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
104
112
105
113
```
106
114
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.
108
116
109
117
> 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`
110
118
@@ -113,8 +121,11 @@ The URL structure for the request is `https://api.github.com/users/<username>` a
113
121
First, let's create a function that does the AJAX call to the GitHub API.
114
122
115
123
```js
116
-
functiongetGithubInfo(user) {
117
-
//call to API
124
+
functiongetGithubInfo(username) {
125
+
var xhr =newXMLHttpRequest();
126
+
xhr.open(...);
127
+
128
+
return xhr;
118
129
}
119
130
```
120
131
@@ -125,11 +136,10 @@ To test this out, let's handle the keypress on the input field. We want this to
// assign getGithubUserInfo(username) to a variable response
132
-
142
+
// assign getGithubUserInfo(username) to a variable response
133
143
}
134
144
})
135
145
});
@@ -161,9 +171,8 @@ The `showUser(user)` function should:
161
171
162
172
2. Add a link to the user's Github profile in `#profile .information`. The link should have a class `profile`
163
173
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.
167
176
168
177
> Don't forget to call `showUser()` from the function handling the keypress!
0 commit comments