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

212
Vistas
“TypeError: Failed to fetch” when the request hasn’t actually failed Error

I'm using an API to make my login page validate the user and password with this code:

function ola() {

var myHeaders = new Headers();

myHeaders.append("Content-Type", "text/plain");


//WITH USERNAME AND PASSWORD LIKE THIS VVVVVV
var raw = "{\r\n  \"password\": \"Olasou1!\",\r\n  \"username\": \"bernardo\"\r\n}\r\n";



var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://api.secureme.pt/api/v1/auth/login", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

};

I get a success message so I know that it worked

But when I try to use the user inputs on the variable raw like this:

function ola() {
  var user = document.getElementById("user").value;
  var password = document.getElementById("password").value;

  var myHeaders = new Headers();
  myHeaders.append("Content-Type", "text/plain");
  


  var raw = {
      password: password,
      username: user
  };
  


  var requestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: raw,
    redirect: 'follow'
  };
  
  fetch("https://api.secureme.pt/api/v1/auth/login", requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log('error', error));

};

And when I put the same username and password, I get an error saying that it failed to fetch:

error TypeError: Failed to fetch
    at ola (:5500/js/loginRegister.js:36:3)
    at HTMLButtonElement.onclick (loginRegister.html:22:74)

I can't figure out why, can someone help me with this?

This is where I call the function on html file:

<button type="submit" class="submit-btn" onclick="ola()">Log In</button>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to serialize your raw data:

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: JSON.stringify(raw),
  redirect: 'follow'
}

You can check fetch documents for more details about which data types allowed by request body:

  • https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body
  • https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters
about 4 years ago · Juan Pablo Isaza Denunciar
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