当$ http post在方法范围内时,不会调用Success Callback

时间:2015-08-17 07:03:29

标签: javascript angularjs zend-framework angularjs-scope

我正在尝试使用前端的@EventListener和后端的Angular JS来实现登录功能。下面是角度js控制器的代码片段。这不会将用户重定向到相应的页面,在调试时,控件不会进入PHP + Zend部分,而是进入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;
            });
        };
    });

1 个答案:

答案 0 :(得分:0)

我宁愿选择成功/错误回调作为then方法的一部分。 从版本1.4.5的角度文档:

  

$ http遗留承诺方法成功与错误   弃用。请改用标准方法。如果   $ httpProvider.useLegacyPromiseExtensions设置为false然后这些   方法将抛出$ http / legacy错误。

试试这个,虽然如果你说你的代码在闭包之外工作,它可能不是你想要的解决方案:

$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;
});
相关问题