2017-01-14 6 views
0

何が起こっているかは、Angular $ http.post()リクエストでログイン・ルートにアクセスしたときにNode APIに正常にログインできないことです。郵便配達員(残りのapiテストアプリケーション)からテストするとき、かなりうまく動作します。ここで

は、クライアント側のコード(API呼び出し)である:

function($http, $scope) { 
     $scope.$watch('search', function() { 
     fetch(); 
     }); 
     $scope.username = 'xxxxxxxxxxxx'; 
     $scope.password = 'xxxxxxxxxxxxxxxx';   
     function fetch() { 
     $scope.details=""; 
     $http.post("http://apiadress.demo/auth/login?username=" + $scope.username + "&password=" + $scope.password) 
      .then(function(response) { 
      $scope.details = response.data.state; 
      console.log(response); 
      }); 
     } 
    } 

この呼び出しが応答を生成します。

{ 
    "state": "fail", 
    "user": null 
} 

ポストマクライアント応答から同じコールespected作る:

{ 
     "state": "success", 
     "user": { 
     "_id": "xxxxxxxxxxxxxxxxxxxx", 
     "password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
     "username": "xxxxxxxxxx", 
     "__v": 0, 
     "created_at": "2016-12-21T21:57:32.663Z" 
     } 
    } 

私のサーバー側ルーティングコード:

ユーザーを見ることができるようは
//log in 
router.post('/login', passport.authenticate('login', { 
    successRedirect: '/auth/success', 
    failureRedirect: '/auth/failure' 
})); 

//responses whit successful login state 
router.get('/success', function(req, res){ 
    res.send({state: req.user ? 'success' : 'fail', user: req.user ?req.user : null}); 
}); 

authenticetedしているようだが、何のデータは、ユーザーデータを送信しているウェブサイト(ユーザー名から

答えて

2

あぁ... APIが角度から呼び出されたリダイレクトに渡さされていません/パスワード)をURL(これは技術的にはGETリクエスト用)を介して送信しますが、POSTリクエストであるためPOSTデータとして送信する必要があります。これを行う方法は次のとおりです。

$http.post("http://apiadress.demo/auth/login", { 
    username: $scope.username, 
    password: $scope.password 
}).then(success, failure); 
関連する問題