Last active
August 31, 2023 05:35
-
-
Save adjmedina/f51b0092a49160b9de8de3ed3c3a1718 to your computer and use it in GitHub Desktop.
A example fetch in typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Recuerda que el evento debe se convertido a un objeto de tipo HTMLFormElement