From bbfd75199bd492ee75aef9cab8d7ff163a61025c Mon Sep 17 00:00:00 2001 From: SuperSimpleDev Date: Tue, 30 Apr 2024 13:57:58 -0400 Subject: [PATCH] 18g Solution --- 2-copy-of-code/lesson-18/lesson18.html | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/2-copy-of-code/lesson-18/lesson18.html b/2-copy-of-code/lesson-18/lesson18.html index b60a730..368e57f 100644 --- a/2-copy-of-code/lesson-18/lesson18.html +++ b/2-copy-of-code/lesson-18/lesson18.html @@ -49,6 +49,7 @@ postGreeting(); */ + /* async function getAmazon() { try { const response = await fetch('https://amazon.com'); @@ -60,6 +61,34 @@ } } getAmazon(); + */ + + async function postGreeting() { + try { + const response = await fetch('https://supersimplebackend.dev/greeting', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }); + + if (response.status >= 400) { + throw response; + } + + const text = await response.text(); + console.log(text); + + } catch (error) { + if (error.status === 400) { + const errorMessage = await error.json(); + console.log(errorMessage); + } else { + console.log('Network error. Please try again later.'); + } + } + } + postGreeting(); \ No newline at end of file