【发布时间】:2016-01-04 14:27:51
【问题描述】:
我在 AngularJS 中使用来自客户端的 Web RestFUL API。
app.controller('LoginController', [
'$http',
'$cookies',
function($http, $cookies) {
this.credentials = {};
this.http = $http;
this.login = function() {
console.log(this.credentials);
var authdata = btoa(
this.credentials.username +
':' +
this.credentials.password
);
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
console.log($http);
var res = $http.post("http://API_NAME/library");
res.success(function(data, status, header){
alert('Successfull func');
console.log($cookies.get('JSESSIONID'));
});
res.error(function(data, status, headers, config) {
console.log('Error Log');
});
};
},
]);
然后,我有这个 Http headers 响应
Set-Cookie:JSESSIONID=azekazEXAMPLErezrzez; Path=/; HttpOnly
我正在使用 ngCookies 获取 JSESSIONID 值,然后在我的浏览器中手动创建它,但我无法访问它。
我已经在 StackOverflow 中看到了所有关于此的帖子,但它们已被弃用或完全不清楚。
感谢您的关注和帮助。
【问题讨论】:
-
为什么需要在 Angular 中访问 JSESSIONID?该cookie被标记为HttpOnly,即在JS中无法访问。
-
我没有得到自动处理cookie JSESSIONID 的发送。我以为我必须手动完成。评论帮助我理解了这一点。问题解决
标签: javascript angularjs http cookies