From 0e94bad62dc210d9a3edfad5d6e3f21f6e8359b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 17:57:15 +0100 Subject: [PATCH] update httpGet --- snippets/httpGet.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 48eee97a1..a24dafac5 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -1,21 +1,20 @@ ### httpGet -Makes a `GET` request to the passed `URL` using `XMLHttpRequest` web api. +Makes a `GET` request to the passed `URL`. -Explain briefly how the snippet works. +Use `XMLHttpRequest` web api to retrieve data from the server. When the response is ready call the callback function. ```js -const httpGet = url => { +const httpGet = (url, callback) => { const request = new XMLHttpRequest(); request.open("GET", url, true); - request.onload = () => { - const response = JSON.parse(request.responseText); - (request.readyState === 4 && request.status == "200") ? console.log(response) : console.error(response); - } + request.onload = () => callback(request); request.send(); } ``` ```js -functionName('sampleInput') // 'sampleOutput' +httpGet('https://jsonplaceholder.typicode.com/posts', request => { + console.log(request.responseText); +}) // 'Array of 100 items' ```