2017-10-12 3 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) 
     } 
    }); 
} 

문제가 있다면, 내가 POST 요청에 GET 응답 데이터를 보낼 필요가 나는 상태 점검을 수행하고있다 사용자 이름과 암호가 일치하면 성공을 보장해야합니다. 그러나 나는 내 생각이 옳은지 확신 할 수 없다.

내가 뭘 잘못하고 있니?

도움/조언을 크게 주시면 감사하겠습니다.

+2

하면, 사용자 이름과 비밀번호를 확인, 당신은 그나마 ThanhTùng @ 그것을 – Akashii

+0

을 할 수있는 GET 요청을 보낼 필요가있다. plunker를 만들었습니다. https://plnkr.co/edit/G1ld4uMcJ1nWCYNZquuM?p=info – SRK

+0

서버 측 검사를 수행하는 방법을 배우지 못하고 있습니다. 내 예제를 수정할 수 있습니까? 아니면 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

관련 문제