我們使用Spring Security
插件運行外部Grails
服務器應用程序。 前端在本地運行AngularJS
。AJAX請求被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