Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
Extract response text from JavaScript fetch POST request

So I'm doing a POST request with some data I collect from an HTML form. However, I want to use the data the server sent back outside of the fetch call and this is causing me headaches.

The response is a JSON string that can contain the key 'userMessage'. I am able to print this data inside of the fetch call. I then return this data and store it in the variable response. I can log this whole object to the console but I cannot use it the same way as in the fetch call. This is my code:

email = document.forms["login"]["email"].value;
pwd = document.forms["login"]["pwd"].value;

data = JSON.stringify({'email':email, 'password': pwd});

response = fetch(
    "https://companyname/path", {
        method: 'POST',
        headers:
        {
            'accept': 'application/json, text/plain, */*',
            'accept-language': '[object Object]',
            'app-version': '1.0.3',
            'content-type': 'application/json'
        },
        body: data
    })
.then(res => res.json())
.then(data => {
    console.log('Success:', data['userMessage']);   //  This works
    return data;
})
.catch((error) => {
    console.error('Error:', error);
});

console.log(response);  //  This works too
alert(response['userMessage']); //  This just shows an alert saying "undefined"

Why doesn't this work? How do I go around it?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda