Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

405
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda