Я вызываю API с угловой передней панели JS, и получаю 401 Несанкционированную ошибку. Вот варианты, которые я пробовал.
Опция 1:
$http({
method: "GET",
xhrFields: {
withCredentials: true
},
headers: {
Authorization: 'Basic ' + btoa('user:password')
},
url: 'http://localhost:11000/api/ping'
}).then(function(r) {
return Session.userId = r.headers('user');
})
Не могли бы вы помочь мне передать учетные данные для поддержки API?
если вы делаете запрос $ http, вам нужно использовать обещание из-за асинхрона
function httpReq (){
return $http({ // this will return a promise
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log('success');
return response;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('error');
return response;
});
}
и как использовать обещание, возвращаемое функцией
httpReq().then( function (res) {
console.log(res);
});