Volver a la lección

Archivo funcional original

public/app.js

El código y las páginas de ejemplo funcionales conservan sus identificadores y textos de muestra originales. La lección y las instrucciones de los archivos los explican en tu idioma.

document.querySelector("form").addEventListener("submit", async (e) => {
  e.preventDefault();
  const f = e.target,
    s = document.querySelector("[role=status]"),
    b = f.querySelector("button");
  b.disabled = true;
  try {
    const r = await fetch("/.netlify/functions/mini-feedback", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + f.elements.access.value,
      },
      body: JSON.stringify({ message: f.elements.message.value }),
    });
    if (!r.ok) throw Error();
    s.textContent = "Stored by the database. Verify the matching message in your table.";
  } catch {
    s.textContent = "The write was not confirmed. Check the practice endpoint.";
  } finally {
    b.disabled = false;
  }
});