使用AngularJS和Spring Security取消AJAX请求

时间:2013-11-28 11:19:51

标签: ajax spring angularjs grails

我们正在使用Grails插件运行外部Spring Security服务器应用程序。 前端在AngularJS上本地运行。

每当我尝试登录时,请求立即被取消。值得注意的是,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 个答案:

答案 0 :(得分:1)

在我的情况下,当AngularJS查询时,显然Chrome无法有效处理302 Moved temporarily状态代码。 Firefox正确显示有一个响应,Chrome只是将请求显示为已取消而没有任何响应信息。

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

相关问题