相关疑难解决方法(0)

由于重定向,设计 JSON API 返回 200 而不是 401

我在 Rails API 上运行 Angular。对于身份验证,我正在拦截 401 状态:

.config(['$httpProvider', function($httpProvider){
    // Prevent rails from making new sessions each time
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');

    var interceptor = ['$location', '$rootScope', '$q', function($location, $rootScope, $q) {
      function success(response) {
        return response;
      }

      function error(response) {
        if (response.status === 401) {
          $rootScope.$broadcast('event:unauthorized');
          $location.path('/login');
          return response;
        }
        return $q.reject(response);
      }

      return function(promise) {
        return promise.then(success, error);
      };
    }];
    $httpProvider.responseInterceptors.push(interceptor);
  }])
Run Code Online (Sandbox Code Playgroud)

但是拦截器没有触发,因为重定向显示状态为 200

Started GET "/api/v1/stages" for 127.0.0.1 at 2014-02-23 04:38:12 +0530
Processing by StagesController#index as HTML
Completed …
Run Code Online (Sandbox Code Playgroud)

json ruby-on-rails devise angularjs

3
推荐指数
1
解决办法
2711
查看次数

标签 统计

angularjs ×1

devise ×1

json ×1

ruby-on-rails ×1