Answer the question
In order to leave comments, you need to log in
Why is it throwing undefined inside XMLHttpRequest?
function get_manager(manager_id) {
var xhr_one = new XMLHttpRequest();
var name_manager = '';
xhr_one.open('get', 'http://localhost:8000/get_managers?mid=' + manager_id + '&secure=' + localStorage.getItem('secure'), true);
xhr_one.send();
xhr_one.addEventListener('load', function () {
name_manager = JSON.parse(xhr_one.response);
return name_manager.fullname;
});
}
Answer the question
In order to leave comments, you need to log in
You can make the get_manager function return a promise:
function get_manager(manager_id) {
return new Promise(function (resolve, reject) {
var xhr_one = new XMLHttpRequest();
var name_manager = '';
xhr_one.open('get', 'http://localhost:8000/get_managers?mid=' + manager_id + '&secure=' + localStorage.getItem('secure'), true);
xhr_one.send();
xhr_one.addEventListener('load', function () {
name_manager = JSON.parse(xhr_one.response);
resolve(name_manager.fullname);
});
xhr_one.addEventListener('error', function () {
reject({
status: this.status,
statusText: xhr_one.statusText
});
});
}
}
get_manager(manager_id).then((name_manager) => {
console.log(name_manager);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question