From 0be75e4b399bd287106ddb8c0b3c8c62fc5e4d9b Mon Sep 17 00:00:00 2001 From: leodaher Date: Mon, 17 Jun 2019 19:19:33 -0300 Subject: [PATCH] feat(curriculum): Add fetch challenge to the current curriculum. (#36033) * Add fetch challenge for the current curriculum --- .../_meta/json-apis-and-ajax/meta.json | 4 + ...t-json-with-the-javascript-fetch-method.md | 167 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md diff --git a/curriculum/challenges/_meta/json-apis-and-ajax/meta.json b/curriculum/challenges/_meta/json-apis-and-ajax/meta.json index 01cda46394..d72d75c6ff 100644 --- a/curriculum/challenges/_meta/json-apis-and-ajax/meta.json +++ b/curriculum/challenges/_meta/json-apis-and-ajax/meta.json @@ -24,6 +24,10 @@ "587d7fae367417b2b2512be3", "Get JSON with the JavaScript XMLHttpRequest Method" ], + [ + "5ccfad82bb2dc6c965a848e5", + "Get JSON with the JavaScript fetch method" + ], [ "587d7fae367417b2b2512be4", "Access the JSON Data from an API" diff --git a/curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md b/curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md new file mode 100644 index 0000000000..25e2a57950 --- /dev/null +++ b/curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md @@ -0,0 +1,167 @@ +--- +id: 5ccfad82bb2dc6c965a848e5 +title: Get JSON with the JavaScript fetch method +challengeType: 6 +--- + +## Description +
+Another way to request external data is to use the JavaScript fetch() method. It is equivalent to XMLHttpRequest, but the syntax is considered easier to understand. +Here is the code for making a GET request to /json/cats.json + +```js + +fetch('/json/cats.json') + .then(response => response.json()) + .then(data => { + document.getElementById('message').innerHTML = JSON.stringify(data); + }) + +``` + +Take a look at each piece of this code. + +The first line is the one that makes the request. So, fetch(URL) makes a GET request to the URL specified. The method returns a Promise. + +After a Promise is returned, if the request was successful, the then method is executed, which takes the response and converts it to JSON format. + +The then method also returns a Promise, which is handled by the next then method. The argument in the second then is the JSON object you are looking for! + +Now, it selects the element that will receive the data by using document.getElementById(). Then it modifies the HTML code of the element by inserting a string created from the JSON object returned from the request. +
+ +## Instructions +
+Update the code to create and send a "GET" request to the freeCodeCamp Cat Photo API. But this time, using the fetch method instead of XMLHttpRequest. +
+ +## Tests +
+ +```yml +tests: + - text: Your code should make a GET request with fetch. + testString: assert(code.match(/fetch\s*\(\s*('|")\/json\/cats\.json\1\s*\)/g)); + - text: Your code should use then to convert the response to JSON. + testString: assert(code.match(/\.then\s*\(\s*(response|\(\s*response\s*\))\s*=>\s*response\s*\.json\s*\(\s*\)\s*\)/g)) + - text: Your code should use then to handle the data converted to JSON by the other then. + testString: assert(code.match(/\.then\s*\(\s*(data|\(\s*data\s*\))\s*=>\s*{[^}]*}\s*\)/g)) + - text: Your code should get the element with id message and change its inner HTML to the string of JSON data. + testString: assert(code.match(/document\s*\.getElementById\s*\(\s*('|")message\1\s*\)\s*\.innerHTML\s*=\s*JSON\s*\.\s*stringify\s*\(\s*data\s*\)/g)); + +``` + +
+ +## Challenge Seed +
+ +
+ +```html + + +

Cat Photo Finder

+

+ The message will go here +

+

+ +

+``` + +
+ + + +
+ +## Solution +
+ +```html + + +

Cat Photo Finder

+

+ The message will go here +

+

+ +

+``` + +