Spaces:
Sleeping
Sleeping
| 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" }); | |