Angular无法读取http post响应中的cookie
Angular cannot read cookies in http post response
类似的问题在这里和许多其他地方都得到了回答。解决方案是设置调用摘要的超时,以便响应cookie与浏览器同步。我在1.2.8中让这个解决方案完美地工作,但在1.3.14中被破坏了。你知道这是否已经改变了吗?
我没有在这里发布任何代码,因为问题与提供的链接中的问题相同,并且它在1.2.8 Angular版本中工作。
以下是我的拦截器从1.2.8更改为1.3.14。该解决方案在1.2.28中也起作用,但仅在1.3.x 中失败
angular.module('localezeServices')
.factory('httpResponseInterceptor', function ($q, $injector, $timeout) {
return function (promise) {
var success = function (response) {
var AuthService = null;
if (!AuthService) { AuthService = $injector.get('AuthService'); }
if(response.headers()['content-type'] === "application/json;charset=utf-8"){
console.log('Intercepting HTTP response.' + response.data.responseStatus);
var data = response.data
if(data.status === "success") {
$timeout(function(){});
return response;
}
else {
if(data.responseStatus === "UNAUTHORIZED"){
AuthService.redirectToLogin();
}
return $q.reject(response);
}
} else {
return response;
}
};
var error = function (response) {
if (response.status === 401) {
AuthService.redirectToLogin();
}
return $q.reject(response);
};
return promise.then(success, error);
};
});
1.3.14
angular.module('localezeServices')
.factory('daHttpInterceptor', function ($q, $injector, $timeout) {
return {
// optional method
'request': function(config) {
if(config.url.indexOf('pendingdomains')===-1){
return config;
}
console.log("Intercepting request for " + config.url);
var AuthService = null;
if (!AuthService) { AuthService = $injector.get('AuthService'); }
if(AuthService.checkUser() === false){
AuthService.redirectToLogin();
}
return config;
},
// optional method
'requestError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// optional method
'response': function(response) {
var AuthService = null;
if (!AuthService) { AuthService = $injector.get('AuthService'); }
if(response.headers()['content-type'] === "application/json;charset=utf-8"){
console.log('Intercepting HTTP response.' + response.data.responseStatus);
var data = response.data
if(data.status === "success") {
// Need a digest for cookies to sync with browser.
$timeout(function() {
console.log('Received success from server.');
}, 100);
return response;
}
else {
if(data.responseStatus === "UNAUTHORIZED"){
AuthService.redirectToLogin();
}
return $q.reject(response);
}
} else {
return response;
}
},
// optional method
'responseError': function(rejection) {
if (response.status === 401) {
AuthService.redirectToLogin();
}
return $q.reject(response);
}
};
});
根据Angular项目中的GitHub问题,解决方案是使用$browser.cookies()
而不是$cookies
,后者将在未来的1.4版本中弃用。
相关文章:
- 在jquery中为显示/隐藏设置cookie
- 如何将getJson的响应保存在全局变量中
- 按下按钮时保存cookie
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 为什么新js Fetch标准禁止响应头名称为'设置Cookie 1/2'
- Angular无法读取http post响应中的cookie
- JQuery-从响应中获取Cookie
- Chrome浏览器:未从响应标头设置cookie
- 何时在浏览器中设置来自服务器的cookie响应
- 我可以在 jsonp 请求的响应中设置 cookie 吗?
- 为什么我的角度响应拦截器没有列出 cookie
- 如何在发送响应节点js(Sails)之前设置cookie
- 浏览器不设置cookie,虽然我有Set-Cookie响应
- 在响应中设置cookie头,但在浏览器中不设置
- 是否可以在JavaScript中从HTTP响应头获取Set-Cookie值?
- retify GET请求返回响应cookie,但未写入浏览器
- NodeJS -如何从服务器响应中获取cookie
- 根据cookie中的数据对JSON响应进行排序
- 浏览器中提供响应cookie
- 获取响应未设置浏览器 Cookie