1 | import { invocaServicio } from "./invocaServicio.js" |
2 | |
3 | /** |
4 | * Envía los datos de la forma a la url usando la codificación |
5 | * multipart/form-data. |
6 | * @param {string} url |
7 | * @param {Event} event |
8 | * @param { "GET" | "POST"| "PUT" | "PATCH" | "DELETE" | "TRACE" | "OPTIONS" |
9 | * | "CONNECT" | "HEAD" } metodoHttp |
10 | */ |
11 | export function submitForm(url, event, metodoHttp = "POST") { |
12 | event.preventDefault() |
13 | const form = event.target |
14 | if (!(form instanceof HTMLFormElement)) |
15 | throw new Error("event.target no es una form.") |
16 | return invocaServicio(fetch(url, { |
17 | method: metodoHttp, |
18 | headers: { "Accept": "application/json, application/problem+json" }, |
19 | body: new FormData(form) |
20 | })) |
21 | } |
22 | |
23 | // Permite que los eventos de html usen la función. |
24 | window["submitForm"] = submitForm |