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

252
Vistas
How do I redirect the page if the fetch api returns status code 302

I have a button with functionality that requires the user to authenticate. If the user is not authenticated the link should redirect to the login page. When I click the button, from the devtools network tab I can see the fetch api first calls the original url which returns a status code 302 and then another call which returns the login page. So Instead of calling the login page after a 302 status code, I want to redirect the page there.

I have tried to use the location header but the result is always null and also for some reason the if(res.status == 302) block never executes

Here is the code

fetch(`https://example.com`)
    .then(res => {
        if(res.status == 302) {
            window.location.href = res.headers.get('location');
        } 
        else if (res.ok) return res.text();
        else console.log(`Error: ${res.status}`);
    })
    .then(data => {
        // Do something
    })
    .catch(e => {
        console.error(e);
    });
});
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