[英]POST request problems during JSON response using AngularJS
使用JSON
服務器登錄表單時出現問題。 我有登錄憑據(如Username and password
並嘗試執行它。 我在服務器上使用了POST
,指定了用戶插入的用戶名和密碼。 服務器應在指定條件下執行查詢,以檢查是否有任何具有該名稱和密碼的行。 如果是,則返回true,如果否,則返回false。 然后客戶端應解析此響應。
這是我的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)
}
});
}
});
我從服務器正確獲得響應。 但是當我使用if
條件使用POST
請求匹配數據時,我不明白我在做什么錯誤?
任何幫助/建議將不勝感激。
我不明白您的問題是服務器端還是客戶端? 是你的問題
if (username === username && password === password) {
console.log(response);
}
您可以使用==插入====
您正在將數據與其自身進行比較。
username === username && password === password
應該
username === $scope.loginfo.username && password === $scope.loginfo.password
盡管如此,任何類型的安全驗證都應該在服務器上而不是在客戶端上進行。
另外,您似乎對Angular還是陌生的。 讓我向您推薦John Papa的風格指南 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.