简体   繁体   English

角$ http vs工厂调用http请求

[英]angular $http vs factory to call http request

Can someone point out whythese two codes doesn't work the same way? 有人可以指出为什么这两个代码不能以相同的方式工作吗? $http.post request works fine and gives me the response back on the other hand using a factory to do the post request it gives me [object object] response but network payload shows the username and password . $http.post要求工作正常,并给我的response使用工厂做回另一方面post要求它给我的翻译:响应,但网络负载显示usernamepassword

html: 的HTML:

<input type="text" class="form-control" id="username" placeholder="Username" ng-model="vm.userlogin.username">

<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="vm.userlogin.password">

straight from controller: 直接来自控制器:

$http.post('/api/login/', vm.userlogin).success(function(response){
            localStorage.setItem('key', response);
            $rootScope.isLoggedIn = true;
            $location.path('/dashboard');
        }).error(function(error){
            console.log('err');
        });

using factory: 使用工厂:

signupService.login(vm.userlogin.username, vm.userlogin.password)
            .then(function(response){
                console.log(response);
                localStorage.setItem('key', response);
                $rootScope.isLoggedIn = true;
                $location.path('/dashboard');
        });

login factory: 登录工厂:

function _login(username, password){
        var data = {
            username: username,
            password: password
        };
        return $http({
            method: 'POST',
            url: '/api/login/',
            data: data
        });
    };

response object in first then() of $http promise is not the same as in success() . $http promise第一个then()response对象与success() Your data is in the data property of it 您的数据在其data属性中

Try using response.data 尝试使用response.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM