【问题标题】:POST request problems during JSON response using AngularJS使用 AngularJS 在 JSON 响应期间的 POST 请求问题
【发布时间】:2017-10-10 21:42:34
【问题描述】:

我在使用JSON 服务器登录表单时遇到问题。我有像Username and password 这样的登录凭据并试图执行它。我在服务器上使用了POST,指定了用户插入的用户名和密码。服务器应在指定条件上执行查询,以检查是否有任何具有该名称和密码的行。如果是则返回真,如果是假则返回假。那么客户端应该解析这个响应。

这是我的 HTML 代码:

<form ng-submit="loginform(logcred)" name="logform"><br/><br>
<tr ng-repeat="logcred in loginfo"></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>  
</form>

这是使用 AngularJS 的 Javascript 代码:

var myApp = angular.module('myApp', []);

myApp.controller('credientials', function($scope, $http) {
$http.get('http://localhost:3000/loginfo')
 .then(
 function successCallback(response){
 $scope.loginfo == response.data;

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

我正在从我的服务器正确获得响应。但是当我使用POST 使用if 条件匹配数据时,我不明白我在做什么错误?

任何帮助/建议将不胜感激。

【问题讨论】:

  • 你在response.data得到了什么?
  • get 方法中,我从我的服务器获取用户名和密码。在post 中,我从用户那里获取输入的用户名和密码

标签: javascript angularjs json


【解决方案1】:

我不明白你的问题是服务器端还是客户端?你的问题是在

if (username === username && password === password) {
console.log(response);

}

你可以使用 == insted of ====

【讨论】:

  • 它在客户端
【解决方案2】:

您正在将数据与自身进行比较。

username === username && password === password

应该是

username === $scope.loginfo.username && password === $scope.loginfo.password

不过,任何类型的安全验证都应该在服务器上而不是在客户端上完成。

另外,您似乎是 Angular 的新手。让我向您推荐John Papa's Styleguide

【讨论】:

  • 是的。我要新的角度。我尝试了您发布的代码,但我得到了未定义的loginfo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
相关资源
最近更新 更多