2013-11-28 93 views
2

我们使用Spring Security插件运行外部Grails服务器应用程序。 前端在本地运行AngularJSAJAX请求被AngularJS和Spring Security取消

每当我尝试登录,请求立即取消..显着AngularJS发送一个GET请求首先与OPTIONS方法;这返回一个200 OK响应就好了。 实际的POST请求永远不会到达服务器,但是有什么可能取消我的请求?

下面的代码:

$scope.login = function() { 
    $http.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; 

    $scope.loggingIn = true; 

    // Setup Config 
    var data = { 
     j_username: $scope.user.email, 
     j_password: $scope.user.password 
    } 
    var config = {method: 'POST', url: serverUri+'/j_spring_security_check/', data: data}; 

    // Dispatch HTTP Request 
    $http(config) 
    .success(function(data, status, headers, config) { 
     if (data.status) { 
      // successful login 
      User.isLogged = true; 
      User.username = data.username; 
     } 
     else { 
      User.isLogged = false; 
      User.username = ''; 
     } 
     $scope.loggingIn = false; 
     console.log("NOICE!"); 
    }) 
    .error(function(data, status, headers, config) { 
     $scope.loggingIn = false; 
     User.isLogged = false; 
     User.username = ''; 

     if (status == 0) { 
      // Request got cancelled 
      console.log("Request got cancelled."); 
      return; 
     } 
    }); 
} 

这是取消要求是什么样子:http://i.stack.imgur.com/kiWnb.png

这是OPTIONS要求是什么样子:http://i.stack.imgur.com/FAj96.png

回答

1

显然Chrome不处理302 Moved temporarily状态码在AngularJS查询我的情况时有效。 Firefox正确显示,Chrome只会将请求显示为已取消,并且没有任何响应信息。

这个问题已经解决了,但是为什么AngularJS不起作用仍然是个谜。在这里看到我的问题: AngularJS $http ajax does not follow Location header