2016-09-14 69 views
0

我正在尝试编写登录用户的帖子请求,但没有任何作用。我不明白为什么,并会为任何帮助感到高兴!谢谢!!Angular - 无法发送POST请求登录用户,不知道什么是错的?

我的控制器:

'use strict'; 
app.controller('loginController', function($scope, $state, $http){ 
    $scope.submitLogin=function(email,password) 
     return $http.post('/login', {email:$scope.email, password:$scope.password }) 

//因为我有NG-模型=“电子邮件” - 我实际上并不需要在$ scope.email通过,但只能用电子邮件传递?!在HTML的

  .then(function(success){ 
      if (sucess){ 
      console.log(success) 
      $state.go('stories') 
      } 
      else { 
      console.log('error') 
      } 
      }) 
     .catch(function(err){ 
     console.log("bad") 
     }) 
     }) 

部分:

<div class="form-group"> 
    <label ng-model="email">email</label> 
    <input type="text" class="form-control" required /> 
    </div> 
    <div class="form-group"> 
    <label ng-model="password">password</label> 
    <input type="password" class="form-control" required /> 
    </div> 
    <button type="submit" class="btn btn-block btn-primary" ng-click="submitLogin()">login</button> 
</form> 

国家:

'use strict'; 
app.config(function ($stateProvider) { 
    $stateProvider.state('login', { 
    url: '/login', 
    controller:'loginController', 
    templateUrl: '/browser/app/login/login.html' 
    }); 
}); 
+0

您需要将ng-model =“email”放入您的输入中而不是标签中。 –

+0

让我知道当你把ng模型输入到输入时它是否仍然不起作用。 –

+0

谢谢。它仍然无法正常工作,我得到这个奇怪的错误:错误:[ng:areq] http://errors.angularjs.org/1.3.20/ng/areq?p0=loginController&p1=not%20aNaNunction%2C%20got% 20定义 – javascript2016

回答

0

为了您的问题,它看起来像头的问题。我需要更多的细节,如控制台日志,请求细节分析更准确。不过,我正试图给出一个解决方案。您可以替换代码:

return $http.post('/login', {email:$scope.email,password:$scope.password}) 

通过:

return $http.post('/login', {email:$scope.email, password:$scope.password},headers: {'Content-Type': 'application/x-www-form-urlencoded'}) 

我认为它会工作。 还有另一种方式通过在发布请求之前使用下面的代码来设置默认请求标头。

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 
+0

它不起作用,但我也从未包含标题之前 – javascript2016

1

将输入模型放入输入标签。

$scope.submitlogin=function(){ 
     data={ 
       email:$scope.email, 
       password:$scope.password 
       } 
      $http.post('/login',data).success(function(){ 

      }); 
    } 

试试这种方式。它可能有效。