2013-11-28 91 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