$ http帖子在方法范围内时,未调用成功回调 [英] Success Callback not called when $http post is in method scope

查看:65
本文介绍了$ http帖子在方法范围内时,未调用成功回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用前端的Angular JS和后端的PHP + Zend来实现登录功能.以下是angular js控制器的代码段.这不会将用户重定向到适当的页面,并且在调试时,控件不会输入success部分,而是输入error部分with data being NULL.但是如果我将$http部分放在函数外部,则控件至少会进入成功部分,其数据是要重定向的下一页的数据.

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

推荐答案

我宁愿将成功/错误回调作为then方法的一部分. 从1.4.5版的角度文档中:

I would rather go for success/error callbacks to be part of the then method. From angular documentation for version 1.4.5:

$ http的传统承诺方法成功与错误 不推荐使用.请改用标准然后方法.如果 $ httpProvider.useLegacyPromiseExtensions设置为false,则这些 方法将抛出$ http/legacy错误.

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

这篇关于$ http帖子在方法范围内时,未调用成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆