当 $http post 在方法范围内时不调用成功回调
Success Callback not called when $http post is in method scope
我正在尝试在前端使用 Angular JS
并在后端使用 PHP + Zend
来实现登录功能。下面是 angular js 控制器的代码片段。这不会将用户重定向到适当的页面,并且在调试时,控件不会进入 success
部分,而是进入 error
部分 with data being NULL
。然而如果我将$http
部分放在函数之外,至少控件进入成功部分,数据是要重定向的下一页的数据。
我可能遗漏了什么以及实现重定向的正确方法是什么?
在下面的代码中,控件没有进入成功部分。
var myApp = angular.module('myApp',[]);
myApp.controller('loginCtrl', function ($scope, $http) {
$scope.authenticatelogin = function () {
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: $scope.login, password: $scope.password, rememberMe: $scope.remember})
}).success(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
};
});
在下面的代码中,控件进入成功部分。
var myApp = angular.module('myApp',[]);
myApp.controller('loginCtrl', function ($scope, $http) {
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: 'abc@abc.com', password: 'xyzxyz', rememberMe: ''})
}).success(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
});
我宁愿 success/error 回调成为 then
方法的一部分。
来自 angular 版本 1.4.5 的文档:
The $http legacy promise methods success and error have been
deprecated. Use the standard then method instead. If
$httpProvider.useLegacyPromiseExtensions is set to false then these
methods will throw $http/legacy error.
试试这个,虽然如果你说你的代码在闭包之外工作,它可能不是你正在寻找的解决方案:
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: $scope.login, password: $scope.password, rememberMe: $scope.remember})
}).then(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}, function(data, status, headers, config) {
$scope.status = status;
});
我正在尝试在前端使用 Angular JS
并在后端使用 PHP + Zend
来实现登录功能。下面是 angular js 控制器的代码片段。这不会将用户重定向到适当的页面,并且在调试时,控件不会进入 success
部分,而是进入 error
部分 with data being NULL
。然而如果我将$http
部分放在函数之外,至少控件进入成功部分,数据是要重定向的下一页的数据。
我可能遗漏了什么以及实现重定向的正确方法是什么?
在下面的代码中,控件没有进入成功部分。
var myApp = angular.module('myApp',[]);
myApp.controller('loginCtrl', function ($scope, $http) {
$scope.authenticatelogin = function () {
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: $scope.login, password: $scope.password, rememberMe: $scope.remember})
}).success(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
};
});
在下面的代码中,控件进入成功部分。
var myApp = angular.module('myApp',[]);
myApp.controller('loginCtrl', function ($scope, $http) {
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: 'abc@abc.com', password: 'xyzxyz', rememberMe: ''})
}).success(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
});
我宁愿 success/error 回调成为 then
方法的一部分。
来自 angular 版本 1.4.5 的文档:
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
试试这个,虽然如果你说你的代码在闭包之外工作,它可能不是你正在寻找的解决方案:
$http({
url: "/admin/auth/authenticatelogin",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({login: $scope.login, password: $scope.password, rememberMe: $scope.remember})
}).then(function(data, status, headers, config) {
$window.location.href= "index/index";
$scope.data = data;
}, function(data, status, headers, config) {
$scope.status = status;
});