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 $btnPeticion = document.querySelector("#btnPeticion"), | |
$resultados = document.querySelector("#resultados"), | |
$inputNombre = document.querySelector("#inputNombre"); | |
$btnPeticion.addEventListener("click", () => { | |
const nombre = $inputNombre.value; | |
if (!nombre) return alert("Escribe tu nombre"); | |
const fd = new FormData(); | |
fd.append("nombre", nombre); | |
fd.append("otroValor", "Otro valor :)"); | |
$resultados.textContent = "Cargando..."; | |
fetch("https://httpbin.org/post", { | |
method: "POST", // Indicar método POST | |
body: fd,// Formulario | |
}) | |
.then(resultadoRaw => { | |
// Lo decodificamos como json | |
return resultadoRaw.json(); | |
}) | |
.then(resultadoComoJson => { | |
// Convertir a cadena | |
$resultados.textContent = JSON.stringify(resultadoComoJson.form); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment