From 1adaf55f077d482e171bf38d10da5523f75279ca Mon Sep 17 00:00:00 2001 From: janic Date: Wed, 8 Jun 2022 23:23:57 +0200 Subject: [PATCH] Fetch with JS --- webui/index.html | 6 ++---- webui/index.js | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/webui/index.html b/webui/index.html index 4008662..5b9808b 100644 --- a/webui/index.html +++ b/webui/index.html @@ -15,10 +15,8 @@

Video Downloader

-
- - -
+ +
\ No newline at end of file diff --git a/webui/index.js b/webui/index.js index 3babd81..184cd0f 100644 --- a/webui/index.js +++ b/webui/index.js @@ -23,16 +23,29 @@ function ytdl() { else { alert("URL is not compliant with this website - I am kinda sorry(Just kidding)"); } - console.log(ytid); add_db(ytid); return; } function add_db(ytid) { - const base_url = "http://127.0.0.1:8000/api/get/"; + var base_url = "http://localhost:8000/api/get/"; var url = base_url.concat(ytid); console.log(url); - fetch(url).then(res => res.json()).then(data => console.log(data)); + send_get(url); + //fetch(url).then(res => res.json()).then(data => console.log(data)); //.catch(res => console.log("Failed: " + res)) - return; + //return; +} +function send_get(url){ + fetch(url) + .then(response => { // indicates whether the response is successful (status code 200-299) or not + if (!response.ok) { + throw new Error(`Request failed with status ${reponse.status}`) + } + else { + console.log(response.body) + } + return response.json() + }) + } \ No newline at end of file