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.
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");