javascript 在 AngularJS 中获取 JSESSIONID 值并创建 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32990836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get JSESSIONID value and create Cookie in AngularJS
提问by Louis Lecocq
I'm using a Web RestFUL API from my client in AngularJS.
我在 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');
});
};
},
]);
Then, i have this Http headers Response
然后,我有这个 Http 标头响应
Set-Cookie:JSESSIONID=azekazEXAMPLErezrzez; Path=/; HttpOnly
I'm using ngCookies to get the JSESSIONID value and then create it manually in my browser, but i can't access it.
我正在使用 ngCookies 来获取 JSESSIONID 值,然后在我的浏览器中手动创建它,但我无法访问它。
I already saw all the posts about this in StackOverflow, but they are deprecated or completely unclear.
我已经在 StackOverflow 中看到了所有关于此的帖子,但它们已被弃用或完全不清楚。
Thanks for your interest and help.
感谢您的关注和帮助。
回答by Quentin
You can't get HttpOnly cookies with JavaScript. That is the whole purpose of the HttpOnly flag.
您无法使用 JavaScript 获取 HttpOnly cookie。这就是 HttpOnly 标志的全部目的。
That said, you shouldn't need to. The cookie should be sent automatically with any request to the same server. If you are dealing with cross-origin XHR requests, set your_XHR_instance.withCredentials
to true
to include cookies.
也就是说,你不应该需要。cookie 应随任何请求自动发送到同一服务器。如果你正在处理跨域XHR请求,设置your_XHR_instance.withCredentials
到true
包括饼干。