-1
I have a function that executes an AJAX, the AJAX response I return in the 'Return', but when I give a console.log the function returns I get no data. Below is the code.
function getListEmployee() {
var ajax = new XMLHttpRequest();
ajax.open("GET", 'http://localhost/erp/api/v1/employee.php');
ajax.responseType = "json";
ajax.send();
ajax.addEventListener("readystatechange" , function () {
if (ajax.readyState === 4 && ajax.status === 200){
var response = ajax.response;
return response;
}
})
}
const data = getListEmployee()
Even putting it this way, I can’t access the value directly, because I need to use the result in another function.
– Ricardo
You may have to refactor part of your code so that the function that consumes getListEmployee becomes an asynchronous function and can then use await.
– Daniel Mendes