From 75ecde6ce2faab2a3aa4f52d57d3000db18971f6 Mon Sep 17 00:00:00 2001 From: TomM-and-jerry Date: Sat, 16 Oct 2021 18:45:59 -0400 Subject: [PATCH] Added solution code for fetch API prob --- index.html | 12 ++++++++++++ index.js | 23 +++++++++++++++++++++++ template.js | 1 + 3 files changed, 36 insertions(+) create mode 100644 index.html create mode 100644 index.js create mode 100644 template.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..44f2d35 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..ac2c8d2 --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +console.log("About to fetch API"); + +fetch("https://jsonplaceholder.typicode.com/posts") + .then(res => { + console.log(res); + if (!res.ok) { + //means 404 error occurred + return null; + } + + return res.json(); + }) + .then(data => { + console.log(data); + }) + .catch(e => { + if (e instanceof TypeError) { + console.log("Something is wrong with requested server."); + } + else { + console.log("Unanticipated error:\n" + e); + } + }); \ No newline at end of file diff --git a/template.js b/template.js new file mode 100644 index 0000000..c6454c5 --- /dev/null +++ b/template.js @@ -0,0 +1 @@ +fetch("https://jsonplaceholder.typicode.com/posts") \ No newline at end of file