我知道那裏有很多關於重定向問題angularjs在這裏,但至今沒有看到這樣的問題,我使用Rails與設計API服務器,我使用ajax/json登錄到Devise就好了,但是當我刷新一個頁面的時候我已經登錄了需要記錄的地方看到它,我得到了「重定向循環」錯誤。像這樣: 轉到應用程序0.0.0.0:3000/#/
,需要登錄,所以這將我重定向到:0.0.0.0:3000/#/login
,一旦我登錄並得到重定向到0.0.0.0:3000/#/
,直到那裏一切正常,但如果現在我刷新或去另一個,比如說0.0.0.0:3000/#/events/new
,然後刷新我得到redirect loop
錯誤。「此網頁有重定向循環」與angularjs +軌道+設計
繼承人的代碼:
app.js
angular.module("eventnote", ['eventServices', 'loginServices', 'pruebaS', 'countrySelect']).config(['$httpProvider', function(provider){
provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}]).config(['$httpProvider', function($httpProvider){//start to configure the interceptor
var interceptor = function($q, $location, $rootScope) {
return function(promise){
return promise.then(function(response){return response;}, function(response){
console.log(response.status);
if (response.status == 401){//just the 401 responses
$rootScope.$broadcast('event:unathorized');
$location.path('/login');
return response;
}
return $q.reject(response);
});
}
};
$httpProvider.responseInterceptors.push(interceptor);//add the interceptor
}]).config(["$routeProvider", function($routeProvider){
$routeProvider.when("/events/new", {templateUrl: 'ang/create.html', controller: NewEventsCtrl})
.when("/events/:id/edit", {templateUrl: 'ang/edit.html', controller: UpdateCtrl })
.when("/login", {templateUrl: "ang/login.html", controller: LoginCtrl})
.when("/", {templateUrl: 'ang/home.html', controller: HomeCtrl})
}]);
controller.js:
function HomeCtrl ($scope, Prueba) {
$scope.state = "";
$scope.obj = Prueba.probar(function(re) { $scope.state = re.mensaje}, function(re){ $scope.state = re.data.error;});
$scope.error = $scope.obj.error;
}
function LoginCtrl($scope, $location, Login, Logout){
$scope.user = {};
$scope.login = function(user){
var u = new Login({user:{email: user.email, password: user.password}});
u.$log_in(function(result){
$location.path('/');
});
}
}
services.js:
angular.module('loginServices', ['ngResource'])
.factory('Login', function($resource){
return $resource('users/sign_in.json', {}, {
log_in: {method: 'POST'}
});
}).factory('Logout', function($resource){
return $resource('users/sign_out.json', {},{
log_out: {method: 'DELETE'}
});
});
angular.module('pruebaS', ['ngResource'])
.factory('Prueba', function($resource){
return $resource('api/prueba', {}, {
probar: {method: 'GET'}
});
});
因爲問題只發生WH我登錄我認爲這可能是interceptor
,因爲我沒有認證時我可以刷新很好。希望有人能幫助我,thanks¡