2016-09-14 34 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(){ 

      }); 
    } 

試試這種方式。它可能有效。