function cleartext() { $('#usernametext').val(''); $('#passwordtext').val(''); } function validstring(str) { if (str !== null && str.length > 0) { return true; } return false; } $(document).ready(function () { console.log("Login page is ready."); cleartext(); $('#loginbtn').off('click').on('click', function () { var username = $('#usernametext').val(); var password = $('#passwordtext').val(); if (!validstring(username) || !validstring(password)) { alert("Please enter both username and password."); cleartext(); return; } fetch('/login.html', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ username: username, password: password }) }) .then(response => { //console.log("Received response from server : " + response.status + " " + response.statusText+" is redirected: "+response.redirected); if (response.redirected) { //alert("Login successful, will redirect now."); //console.log("Redirecting to: " + response.url); window.location.href = response.url; return; } if (response.status === 400 || response.status === 401) { alert("Login failed"); cleartext(); } }) .catch(error => { console.error('Error:', error); alert("An error occurred during login."); cleartext(); }); }); });