diff --git a/static/js/main.js b/static/js/main.js index f15327d..06f9172 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,20 +1,27 @@ document.getElementById("notify-button").addEventListener("click", async () => { - const emailInput = document.getElementById("email-input"); - const email = emailInput.value; + const emailInput = document.getElementById("email-input"); + const email = emailInput.value.trim(); - if (email) { - const response = await fetch("/subscribe", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ email }), - }); + if (email) { + try { + const response = await fetch("/subscribe", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email }), + }); - const result = await response.json(); - alert(result.message || result.error); - emailInput.value = ""; // Clear input field - } else { - alert("Please enter a valid email."); + // Check if response is OK, then parse JSON + const result = await response.json(); + console.log("Server response:", result); + alert(result.message || result.error); + } catch (error) { + console.error("Error during subscribe fetch:", error); + alert("There was an error during subscription. Please try again later."); } + emailInput.value = ""; // Clear input field + } else { + alert("Please enter a valid email."); + } }); window.addEventListener('scroll', function() {