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

Angular cannot read cookies in http post response

本文关键字:响应 cookie post http 读取 Angular      更新时间:2023-09-26

类似的问题在这里和许多其他地方都得到了回答。解决方案是设置调用摘要的超时,以便响应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版本中弃用。