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

403
Vistas
JavaScript fetch api: TypeError failed to fetch

I'm creating a blog with a friend as a small project between us. I'm and tested the backend in PHP, he's writing the frontend code. As he is still newer than me HTML and JS, I'm also writing the JS code to handle comms with the backend. The issue we're facing and can't get our heads around is the following: Logging in is possible with username or email and the password. The login code works when

  1. Username - password with curl
  2. Email - password with curl
  3. Username - password with Postman
  4. Email - password with Postman
  5. Username - password with vanilla JS fetch

What doesn't work is the Email - password with vanilla JS fetch. In this case we get the TypeError: Load failed or Failed to fetch. This is the JS code:

    
        const credential = document.getElementById('email').value;
        const password = document.getElementById('password').value;
        const body = JSON.stringify({ credential, password });
    
            fetch('/api/auth/login', { method: 'POST', body: body, headers: { 'Content-Type': 'application/json; charset=UTF-8' } })
                .then((res) => res.json())
                .then((data) => {
                    if (data.access_token) {
                        localStorage.setItem('fit_token', data.access_token);
                    } else {
                        alert(data.message);
                    }
                })
                .catch((err) => console.log(err));
    };

The catch isn't even reached, nothing gets logged in console. I only see the error when I debug...

Can anyone point us in the right direction?

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