http post回调不工作角工厂 [英] http post callback not working angular factory

查看:152
本文介绍了http post回调不工作角工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从角度服务获得回调,但回调函数不起作用。

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

我的服务代码是: -

My service code is:-

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

永远记住服务就像原型功能一样

我们不应该t返回服务中的对象。 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

这篇关于http post回调不工作角工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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