簡體   English   中英

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在Angular項目中的GitHub問題,解決方案是使用$browser.cookies()而不是$cookies ,這將在將來的1.4版本中棄用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM