2016-07-26 110 views
0

有人能指出爲什麼這兩個代碼不能以相同的方式工作嗎? $http.post請求工作正常,並給我0123¾另一方面使用工廠做post請求它給我[對象對象]響應,但網絡負載顯示usernamepassword

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"> 
從控制器

直:

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

使用工廠:

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

登錄工廠:

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

當你使用'。那麼()'不要你需要使用'response.data'獲取的實際數據是

嘗試的data財產? – Lex

+0

什麼是該死的,這是一個簡單的:(謝謝。 – nCore

回答

1

response第一個對象then()$http承諾與success()不一樣。你的數據在使用response.data