2017-10-12 149 views
1

我是使用Angularjs的新手,在解析JSON響應時遇到問題。我正在爲登錄頁面進行客戶端身份驗證,並且我已經使用get請求從服務器端獲取數據,請求客戶端使用post。 HTML代碼:如何將GET響應數據傳遞到POST請求

<form ng-submit="loginform(logcred)" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br> 
<tr ng-repeat="logcred in serverinfo"></tr> 
<div> 
    <label form="emailinput"><b>Email</b></label> 
    <input type="email" class="form-control" name="uname" id="emailinput" placeholder="[email protected]" ng-model="logcred.username" > 
</div> 

<div> 
    <label form="pwdinput"><b>Password</b></label> 
    <input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="logcred.password"> 
</div> 

<div> 
    <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button> 
    <button class="btn btn-primary" ng-click="submit()">Login</button> 
</div> 
<br/> 
</form> 

AngularJS代碼:

var myApp = angular.module('myApp', []); 
    myApp.controller('credientials', function($scope, $http) { 
      /* server side response*/ 
    $http.get('http://localhost:3000/loginfo 
    .then(
    function successCallback(response){ 
    $scope.serverinfo = response.data; 
    });  

/* client-side response*/ 

$scope.loginform = function(serverinfo,username,password){ 
    $http({ 
     url: 'http://localhost:3000/loginfo', 
     method: 'POST', 
     data: { 
      "username" :username, 
      "password" :password, 
      } 
     }) 
     .then(
     function successCallback(response){ 
     console.log(response); 
     if (serverinfo.username === response.data.username && serverinfo.password === response.data.password) { 
     $scope.signinfo = response.data; 
     }else{ 
      console.log("Error: " + response) 
     } 
    }); 
} 

問題,我面對的是什麼,我需要的GET響應數據發送到POST請求,但我在做健康檢查,如果用戶名和密碼匹配,它應該給成功meassage。但我不確定我的想法是否正確。

我在做什麼錯?

任何幫助/建議將不勝感激。

+2

您檢查用戶名和密碼,如果匹配成功發送信息,你不需要發送GET請求做 – Akashii

+0

@ThanhTùng。我創建的plunker:https://plnkr.co/edit/G1ld4uMcJ1nWCYNZquuM?p=info – SRK

+0

我沒有得到如何做服務器端檢查,你可以修改我的plunker,否則,如果你有任何工作的例子下降我@ThanhTùng – SRK

回答

0

您可以嘗試下面的代碼。

$scope.loginform = function(serverinfo,username,password){ 
    $http({ 
     url: 'http://localhost:3000/loginfo', 
     method: 'POST', 
     data: { 
      "username" :username, 
      "password" :password, 
      } 
     }) 
     .then(
     function successCallback(response){ 
     console.log(response); 
     if (response) { // Response will be either true or false. For this yu need to change the API response. 
      //Logged in successfully 
     }else{ 
      //Something went wrong 
     } 
    }); 
} 
在服務器端
+0

是的。我會在你的代碼中嘗試@rakesh – SRK

+0

的迴應 – Akashii

相關問題