简体   繁体   English

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

[英]How to pass GET response data to POST Request

I am new using Angularjs and I am having an issue while parsing a JSON response. 我是使用Angularjs的新手,并且在解析JSON响应时遇到问题。 I am doing client side authentication for the login page and I have used get request to fetch data from servers side and post request for client side. 我正在对登录页面进行客户端身份验证,并且我使用了get请求从服务器端获取数据并向客户端post请求。 HTML code : 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 code : 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)
        }
    });
}

Problem what I am facing is, I need to send the GET response data into POST request and there I am doing the condition check, if the username and password matches, it's should give success meassage. 我面临的问题是,我需要将GET响应数据发送到POST请求中,并且在此进行条件检查,如果用户名和密码匹配,则应该给予成功提示。 But I am not sure my thinking is right or not. 但是我不确定我的想法是否正确。

What am I doing wrong? 我究竟做错了什么?

Any help / advice would be greatly appreciated. 任何帮助/建议将不胜感激。

You can try below code. 您可以尝试以下代码。

$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
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM