使用 AngularJS 响应期间的 POST 请求问题



我在使用服务器登录表单时遇到问题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我不明白我犯了什么错误?

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

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

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

}

您可以使用 == 而不是 ====

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

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

应该是

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

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

另外,您似乎是Angular的新手。让我向您推荐约翰爸爸的风格指南。

最新更新