-1
I need to create two functions.
One of them where I will make a request in Javascript to pick up a Bearer Token, and the second, I will use the token to call another function.
However, I’m not getting the return of the first function.
The code is that way:
function getAuthToken() {
const url = "https://jsonplaceholder.typicode.com/posts";
const params = {
"login" : "admin",
"password" : "123456"
};
$.ajaxSetup({
headers:{
'Accept': "application/json"
}
});
$.post(url, params, function(response, status) {
if(status == 'success') {
return response.token
}
});
}
console.log(getAuthToken())
Every time I give the console.log
to try to catch the result, gives a Undefined value.
But if I give one console.log
within the function itself, without giving the Return, the return is as follows:
{
login:"admin",
password:"123456",
id:101
}
The difficulty I’m having is getting sponse and return to the main function, and then I can get this token and use it in a second function.
You could also declare getAuthToken async, not to use an anonymous function.
– Giuliana Bezerra