Success Callback not called when $http post is in

2019-08-13 07:06发布

问题:

I am trying to achieve a login functionality using Angular JS on front-end and PHP + Zend on the back-end. Below is the code snippet of the angular js controller. This doesn't redirect the user to the appropriate page and while debugging, the control doesn't enter the success part but enters the error part with data being NULL. However if I put the $http part outside the function, atleast the control enters the success part with data being that of the next page to be redirected.

What could i be missing and what is the correct way to achieve the redirection?

In the below code the control doesn't enter the success section.

    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;
            });
        };
    });

In the below code the control enters the success section.

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;
        });
});

回答1:

I would rather go for success/error callbacks to be part of the then method. From angular documentation for version 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.

Try this one, although if you say that your code works outside the closure, it might not be the solution you are looking for:

$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;
});