$ HTTP响应设置Cookie无法访问 [英] $http response Set-Cookie not accessible
问题描述
我目前正在写一个angularjs前端我的后端,和我遇到一个有些常见的问题:
I'm currently writing an angularjs frontend to my backend, and I'm running into a somewhat common issue:
服务器发送的cookie回一个响应,但angular.js完全被忽略(甚至不能打印出设置Cookie的值)。
Server sends a cookie back in a response, but is completely ignored by angular.js (can't even print out the 'Set-Cookie' value).
我试过读
<一个href=\"http://stackoverflow.com/questions/15026016/set-cookie-in-http-header-is-ignored-with-angularjs\">Set-Cookie在HTTP头中与AngularJS 忽略
<一个href=\"http://stackoverflow.com/questions/19383311/angularjs-http-does-not-seem-to-understand-set-cookie-in-the-response\">Angularjs $ HTTP似乎并不明白&QUOT;设置Cookie&QUOT;在响应
但不幸的是,我有尝试了所有的解决方案,只是似乎不工作。
but unfortunately I've tried all the solutions there and just doesn't seem to work.
请求发送
收到响应
我使用angular.js(V1.2.10),这就是我用来做请求
I'm using angular.js (v1.2.10), and this is what I used to make the request
$http.post('/services/api/emailLogin',
sanitizeCredentials(credentials),
{
withCredentials: true
}).success(function(data, status, header) {
console.log(header('Server'));
console.log(header('Set-Cookie'));
console.log(header('Access-Control-Allow-Headers'));
console.log(header('Access-Control-Allow-Methods'));
console.log(header);
}).then(
function(response) {
console.log(response);
return response.data;
});
withCredentials = TRUE
发出请求之前设置在客户端上。
withCredentials=true
is set on the client side before making the request.
访问控制允许的凭据=真
在返回响应之前设置在服务器端。
Access-Control-Allow-Credentials=true
is set on the server side before returning the response.
您可以清楚地看到设置Cookie
是从Chrome开发者工具的响应头,但打印输出仅
You can clearly see Set-Cookie
is in the response headers from Chrome Developer Tools, but printout is just
只有不被打印出来设置Cookie
在响应头。我不知道为什么会发生这种情况?有我,以确保一种方式 withCredentials = TRUE
确实设置(我没有看到它的请求头)?
Only Set-Cookie
in the response header is not being printed out. I'm wondering why does this occur? Is there a way for me to make sure withCredentials=true
is indeed set (I didn't see it in the request header)?
任何帮助AP preciated!
Any help is appreciated!
推荐答案
我看了看里面的 $ httpBackend
源$ C $ C:
I looked inside $httpBackend
source code:
xhr.onreadystatechange = function() {
// some code
if (xhr && xhr.readyState == 4) {
var responseHeaders = null,
response = null;
if(status !== ABORTED) {
responseHeaders = xhr.getAllResponseHeaders();
// some code
它使用 XMLHtt $ P $#pquest getAllResponseHeaders
来获得响应头。
一个小的搜索后,我发现这个问题:<一href=\"http://stackoverflow.com/questions/14686769/xmlhttp-getresponseheader-not-working-for-cors\">xmlHttp.getResponseHeader +不工作的CORS
After a little search I found this question: xmlHttp.getResponseHeader + Not working for CORS
这让我意识到,通过XHR它的规格,不支持设置COOKIE
,所以什么都没有做特别angular.js。
Which made me realize that XHR by it's specification, doesn't support SET-COOKIE
, so it have nothing to do with angular.js in particular.
<一个href=\"http://www.w3.org/TR/XMLHtt$p$pquest/#the-getallresponseheaders%28%29-method\">http://www.w3.org/TR/XMLHtt$p$pquest/#the-getallresponseheaders%28%29-method
返回所有的头从响应,与外的字段名称是设置Cookie或Set-COOKIE2
4.7.4 The getAllResponseHeaders() method
Returns all headers from the response, with the exception of those whose field name is Set-Cookie or Set-Cookie2.
这是一个不同的模块,所以你必须将它添加到您的脚本
Instead just use $cookies
:
It's a different module so you must add it to your scripts
<script src="angular.js"></script>
<script src="angular-cookies.js"></script>
和它添加到模块依赖关系:
And add it to the module dependencies:
var app = angular.module('app',['ngCookies']);
然后,只需使用它像这样:
Then just use it like so:
app.controller('ctrl', function($scope, $http , $cookies, $timeout){
$scope.request = function(){
$http.get('/api').then(function(response){
$timeout(function(){
console.log($cookies.session)
});
})
}
})
我用 $超时
,因为 $饼干
只能用消化后,浏览器的cookie同步。
I use $timeout
because $cookies
only synchronize with browser's cookies after a digest.
这篇关于$ HTTP响应设置Cookie无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!