如何将GET响应数据传递给POST请求

时间:2017-10-12 06:57:12

标签: javascript angularjs

我是使用Angularjs的新手,我在解析JSON响应时遇到了问题。我正在为登录页面进行客户端身份验证,我使用get请求从服务器端获取数据,并post请求客户端。 HTML代码:

    <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="you@example.com" 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。但我不确定我的想法是否正确。

我做错了什么?

非常感谢任何帮助/建议。

1 个答案:

答案 0 :(得分: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
        }
    });
}
相关问题