我有一个GET端点。
它启用了HTTP基本身份验证。我想为给定的端点创建一个GET请求。
https://example.com/api GET
User Name :- admin
Password :- admin
我的代码: -
$scope.listData = function() {
$http.get('https://example.com/api').then(function(response) {
$scope.items = response.data;
});
}
通过身份验证的推荐方法是什么?
GET的第二个参数是标题部分。对于前者
$http.get('www.google.com/someapi', {
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}).then()..;
推荐的方法是http拦截器。拦截器做什么,你可以假设它是一个调用每个api请求和响应的钩子,一旦你编写它,它将自动在每个api请求中添加令牌。下面是你读过这篇文章的网址。
Angular Authentication: Using the Http Client and Http Interceptors