Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

729
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!