Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 3, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/7e689c0043cb0448c37a4422a84ca8a4 to your computer and use it in GitHub Desktop.
Save parzibyte/7e689c0043cb0448c37a4422a84ca8a4 to your computer and use it in GitHub Desktop.
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