Skip to content

Instantly share code, notes, and snippets.

@adjmedina
Last active August 31, 2023 05:35
Show Gist options
  • Save adjmedina/f51b0092a49160b9de8de3ed3c3a1718 to your computer and use it in GitHub Desktop.
Save adjmedina/f51b0092a49160b9de8de3ed3c3a1718 to your computer and use it in GitHub Desktop.
A example fetch in typescript
const enviarFormulario = async (e: Event) => {
e.preventDefault();
if (!ValidaMandatorios()) {
return false;
}
const formulario = e.target as HTMLFormElement;
const formData = new FormData(formulario);
try {
const resultado = await Swal.fire({
title: '¿Quieres guardar los cambios?',
icon: 'question',
showCancelButton: true,
confirmButtonText: 'Sí',
cancelButtonText: 'No',
});
if (resultado.isConfirmed) {
const response = await fetch('/home/index', {
method: 'POST',
body: formData,
});
const data = await response.json();
if (data) {
Swal.fire({
icon: 'success',
title: 'Se ha guardado la información',
showConfirmButton: false,
timer: 1500,
});
} else {
Swal.fire({
icon: 'error',
title: 'Ha ocurrido un problema al guardar la información: ' + data,
showConfirmButton: false,
timer: 1500,
});
}
}
} catch (error) {
console.error('Error:', error);
}
};
@adjmedina
Copy link
Author

Recuerda que el evento debe se convertido a un objeto de tipo HTMLFormElement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment