Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 3, 2019 16:18
Embed
What would you like to do?
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