Fetch with JS

This commit is contained in:
janic 2022-06-08 23:23:57 +02:00
parent 8a9e5881a0
commit 1adaf55f07
2 changed files with 19 additions and 8 deletions

View File

@ -15,10 +15,8 @@
<script src="index.js"></script> <script src="index.js"></script>
<h1>Video Downloader</h1> <h1>Video Downloader</h1>
<center> <center>
<form id="DownloadForm"> <input type="text" name="url" id="url" placeholder="Enter your url here">
<input type="text" name="url" id="url" placeholder="Enter your url here"> <button type="submit" id="downloadbutton" onclick="ytdl()">Download</button>
<button type="submit" id="downloadbutton" onclick="ytdl()">Download</button>
</form>
</center> </center>
</body> </body>
</html> </html>

View File

@ -23,16 +23,29 @@ function ytdl() {
else { else {
alert("URL is not compliant with this website - I am kinda sorry(Just kidding)"); alert("URL is not compliant with this website - I am kinda sorry(Just kidding)");
} }
console.log(ytid);
add_db(ytid); add_db(ytid);
return; return;
} }
function add_db(ytid) { 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); var url = base_url.concat(ytid);
console.log(url); 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)) //.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()
})
} }