Fetch with JS
This commit is contained in:
		@@ -15,10 +15,8 @@
 | 
			
		||||
    <script src="index.js"></script>
 | 
			
		||||
    <h1>Video Downloader</h1>
 | 
			
		||||
    <center>
 | 
			
		||||
        <form id="DownloadForm">
 | 
			
		||||
            <input type="text" name="url" id="url" placeholder="Enter your url here">
 | 
			
		||||
            <button type="submit" id="downloadbutton" onclick="ytdl()">Download</button>
 | 
			
		||||
        </form>
 | 
			
		||||
        <input type="text" name="url" id="url" placeholder="Enter your url here">
 | 
			
		||||
        <button type="submit" id="downloadbutton" onclick="ytdl()">Download</button>
 | 
			
		||||
    </center>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -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()
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user