http post回调不工作角度工厂

问题描述:

我正在尝试从 angular 服务获取回调,但回调函数不起作用.

I am trying to get a callback from the angular service but the callback function does not work.

这是我的控制器代码:-

Here is My controller code:-

$scope.uu = "hello"  // this i just make for test
$scope.login = function(user){
    //console.log(user)
    AuthenticationService.Login(user.username,user.password).success(function(data){
        console.log(data);
        $scope.uu = data;
    })
}

我的服务代码是:-

    mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){
    var service = {};
    service.Login = function (username, password, callback) {

            $http({
        method: "POST",
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'users/authenticate',
        data: {email: username,password:password}
    })
    .success(callback);
        };
        return service;
});

我在控制台中都没有得到响应.范围也没有改变.我已经提到了这个问题,但答案对我不起作用.$http 从服务到控制器的 POST 响应

Neither I get response in console. Neither the scope is changed. I have referred to this question but the answer is not working for me. $http POST response from service to controller

如果您正在编写服务,您的代码应该如下所示:

If you are writing service your code should look like this :

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){
    
    this.Login = function (username, password, callback) {

            $http({
        method: "POST",
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'users/authenticate',
        data: {email: username,password:password}
    })
    .success(callback);
        };
        
});

永远记住服务就像原型函数
我们不应该在服务的 cas 中返回对象.Angular 实例化对象并自动返回

Always remember service is just like prototype function
We should not return object in cas of service. Angular Instantiate the object and return automatically