AngularJS错误.success不是函数

时间:2015-11-04 20:25:42

标签: angularjs

我已经构建了一个工厂来处理我的控制器的功能,但不知何故控制器在其中一个函数上返回错误:

  

错误:Auth.getUser(...)。成功不是一个功能   @ http://localhost:8080/app/controllers/mainCtrl.js:10:1
  ...

我不知道这里发生了什么,其余功能似乎工作得很好?

主控制器:

angular.module('mainCtrl', [])
.controller('mainController', function($rootScope, $location, Auth) {
    var vm = this;
    vm.loggedIn = Auth.isLoggedIn();
    $rootScope.$on('$routeChangeStart', function() {
        vm.loggedIn = Auth.isLoggedIn();
        Auth.getUser()
            .success(function(data) {
                vm.user = data;
            });
    });
    vm.doLogin = function() {
        Auth.login(vm.loginData.username, vm.loginData.password)
            .success(function(data) {
                $location.path('/users');
            });
    };
});

6 个答案:

答案 0 :(得分:96)

请参阅' 弃用通知'来自$http service documentation

  

$ http遗留承诺方法成功错误已经存在   弃用。请改用标准然后方法。

您可以在documentation about $q中了解有关这些方法的更多信息。

答案 1 :(得分:8)

这是代码

已删除代码

$http(  
{  
    method: 'POST',  
    url: '/Home/CreateCustomer',   /*You URL to post*/
    data: $scope.cust   /*You data object/class to post*/
}).success(function (data, status, headers, config)  
{  

}).error(function (data, status, headers, config)  
{  

}); 

允许的代码 Angular $http docs

$http(
    {
       method: 'POST',
       url: '/Home/CreateCustomer',  /*You URL to post*/
       data: $scope.cust  /*You data object/class to post*/
    }).then(function successCallback(response) {
       // this callback will be called asynchronously
       // when the response is available


    }, function errorCallback(response) {
       // called asynchronously if an error occurs
       // or server returns response with an error status.

});

答案 2 :(得分:2)

您可以使用then instate success:

var personController = function ($scope, personService) {
    $scope.persons = personService.getAll().then(function (data) {
        $scope.persons = genericSuccess(data);
    });
    var genericSuccess=function(res) {
        return res.data;
    }
};

答案 3 :(得分:1)

成功承诺现已弃用。然后使用功能代替成功

答案 4 :(得分:0)

无论如何 .success已被弃用是一件好事。你问的原因,我个人从来没有与它和平相处?

object.functionCall(parameters).success(functions (response){

if(response!=="failure")
do this-----
});

我的意思是什么样的成功处理其内部的失败。 使用然后,所有逻辑将开始有意义。

答案 5 :(得分:0)

angular version 1.2.x你可以使用.success 角度版本1.6.x您将收到错误“.success不是函数”

解决方案是:将“.success”替换为“.then”