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

732
Vistas
GET request works in Postman but it doesn´t work as code

I´m doing a petition using postman and it works for me. But when I press the code generation button and I selected javascript - XHR it doesn´t work. The apparently don´t works and I don´t know the reason. Here is the code that postman generates to do the xhr call:

var data = JSON.stringify({
  "login": "Baldan",
  "password": "Hero"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "localhost:6001/usuarios/619420df977695930002adb6");
xhr.setRequestHeader("token", "bearer - eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTk0MjBkZjk3NzY5NTkzMDAwMmFkYjYiLCJsb2dpbiI6IkJhbGRhbiIsInJvbCI6IkNMSUVOVEUiLCJtb3ZpZGEiOiJBQkNERUYiLCJpYXQiOjE2Mzg2NTg3NDd9.9ebMdHDgH45DjA1G5D4KZVqaOTU0GFcu47alVjms2KgkzSdXFTN0DxjCSBzOYl9G1KxkmWNEJHDUhFsdid-SQQ");
xhr.setRequestHeader("Authorization", "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTk0MjBkZjk3NzY5NTkzMDAwMmFkYjYiLCJsb2dpbiI6IkJhbGRhbiIsInJvbCI6IkNMSUVOVEUiLCJtb3ZpZGEiOiJBQkNERUYiLCJpYXQiOjE2Mzg4MTQ5NTd9.B4Cv0TzM0v416jIzTxSm6Ix3kNNE-AJ-W9BWG-N6o4c_WuzsNQuyFDDLBaB8k049lJuSxiZbyNsh2Y41ViaRVw");
xhr.setRequestHeader("Content-Type", "application/json");

xhr.send(data);

The console.log shows an empty string.

Thank you so much for your help Does anyone knows why the excution of this code doesn´t work.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have missed the protocol and it tries to access it as a local relative directory. I believe you need to add the protocol here:

// This is wrong:
xhr.open("GET", "localhost:6001/usuarios/619420df977695930002adb6");
//---------------^
// This is correct:
xhr.open("GET", "http://localhost:6001/usuarios/619420df977695930002adb6");
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