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

时间:2015-03-05 01:50:46

标签: javascript angularjs cookies

类似的问题已被回答here和许多其他地方。解决方案是设置超时以调用摘要,以便响应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);
              }
            };  

    });

1 个答案:

答案 0 :(得分:0)

根据Angular项目中Angular项目中的GitHub问题,解决方案是使用$browser.cookies()代替$cookies,这将在将来的1.4版本中弃用。