Spaces:
Sleeping
Sleeping
File size: 856 Bytes
b6154b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | async function postJson(url, body) {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
return response.json();
}
function bind(formId, resultId, endpoint, extra = {}) {
const form = document.getElementById(formId);
const result = document.getElementById(resultId);
form.addEventListener("submit", async (event) => {
event.preventDefault();
result.textContent = "Procesando...";
const payload = Object.fromEntries(new FormData(form).entries());
const data = await postJson(endpoint, { ...payload, ...extra });
result.textContent = JSON.stringify(data, null, 2);
});
}
bind("product-form", "product-result", "/api/products", { fuente: "web" });
bind("consume-form", "consume-result", "/api/consume", { fuente: "web-consumo" });
|