Angular 无法读取 http post 响应中的 cookie

Angular cannot read cookies in http post response

类似的问题已在 here 和许多其他地方得到回答。解决方案是设置一个超时来调用摘要,以便响应 cookie 与浏览器同步。我让这个解决方案在 1.2.8 中完美运行,但在 1.3.14 中被破坏了。知道这是否改变了吗?

我没有在此处发布任何代码,因为问题与 link 提供的问题相同,并且在 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 项目中的 Angular 问题,解决方案是使用 $browser.cookies() 而不是 $cookies,后者将在未来 1.4 中弃用版本。