update httpGet
This commit is contained in:
@ -3,22 +3,19 @@
|
|||||||
Makes a `GET` request to the passed URL.
|
Makes a `GET` request to the passed URL.
|
||||||
|
|
||||||
Use `XMLHttpRequest` web api to make a `get` request to the given `url`.
|
Use `XMLHttpRequest` web api to make a `get` request to the given `url`.
|
||||||
Handle the `onload` event, by running the provided `callback` function.
|
Handle the `onload` event, by console logging the `responseText`.
|
||||||
Handle the `onerror` event, by running the provided `err` function.
|
Handle the `onerror` event, by running the provided `err` function.
|
||||||
Omit the third argument, `err` to log the request to the console's error stream by default.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const httpGet = (url, callback, err = console.error) => {
|
const httpGet = (url, err = console.error) => {
|
||||||
const request = new XMLHttpRequest();
|
const request = new XMLHttpRequest();
|
||||||
request.open("GET", url, true);
|
request.open("GET", url, true);
|
||||||
request.onload = () => callback(request);
|
request.onload = () => console.log(request.responseText);
|
||||||
request.onerror = () => err(request);
|
request.onerror = () => err(request);
|
||||||
request.send();
|
request.send();
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
httpGet('https://website.com/users', request => {
|
httpGet('https://jsonplaceholder.typicode.com/posts'); // 'Console logs JSON of 100 posts'
|
||||||
console.log(request.responseText);
|
|
||||||
}); // 'Retrieves all users from the database'
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user