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

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

问题描述

我正在尝试使用前端的 Angular JS 和后端的 PHP + Zend 来实现登录功能.下面是 angular js 控制器的代码片段.这不会将用户重定向到适当的页面,并且在调试时,控件不会进入 success 部分,而是进入 error 部分 数据为 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 遗留的 promise 方法成功和错误已经已弃用.请改用标准 then 方法.如果$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 post 在方法范围内时不调用成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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