角JS应用程序始终打如果语句从不去其他语句
问题描述:
我正在使用Wcf服务到Angular JS应用程序。我正在通过提供用户名和密码来创建用户登录系统。但是,我在输入中输入的任何用户名或密码都会提示其始终显示消息用户名和密码是否正确。我想如果用户名和密码是正确的,那么我想在角度js应用程序中显示消息,否则用户名和密码是不正确的,但我不知道为什么它总是击中if语句,并没有关系,如果我输入错误的用户名或密码。角JS应用程序始终打如果语句从不去其他语句
这是我的脚本代码。
///// <reference path="../angular.min.js" />
var app = angular.module("WebClientModule", [])
.controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {
$scope.OperType = 1;
//1 Mean New Entry
//To Clear all input controls.
function ClearModels() {
$scope.OperType = 1;
$scope.Username = "";
$scope.Password = "";
}
$scope.login = function() {
var User = {
Username: $scope.Username,
Password: $scope.Password,
};
myService.AuthenticateUser(User).then(function (pl) {
if (pl.data) {
$scope.msg = "Password or username is correct !";//Always hit on this line
}
else {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
}
}, function (err) {
$scope.msg = "Password or username is Incorrect !";
console.log("Some error Occured" + err);
});
};
}]);
app.service("myService", function ($http) {
this.AuthenticateUser = function (User) {
return $http.post("http://localhost:52098/HalifaxIISService.svc/AuthenticateUser", JSON.stringify(User));
}
})
这是我的HTML代码..
@{
Layout = null;
}
<!DOCTYPE html>
<html ng-app="WebClientModule">
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/RegistrationScript/LoginScript.js"></script>
</head>
<body>
<table id="tblContainer" data-ng-controller="Web_Client_Controller">
<tr>
<td></td>
</tr>
<tr>
<td>
<div style="color: red;">{{msg}}</div>
<table style="border: solid 4px Red; padding: 2px;">
<tr>
<td></td>
<td>
<span>Username</span>
</td>
<td>
<input type="text" id="username" data-ng-model="Username" required="" />
</td>
</tr>
<tr>
<td></td>
<td>
<span>Password</span>
</td>
<td>
<input type="password" id="password" required data-ng-model="Password" require="" />
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input type="button" id="Login" value="Login" data-ng-click="login()" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<script src="~/RegistrationScript/LoginScript.js"></script>
这里是放出来..
答
在then()块中,首先尝试使用以下代码 console.log(pl.data)检查pl.data的值。 有了这个,你将会知道从WCF服务返回的值。 如果服务始终返回True,那么在您的Angular中,它将始终执行if()语句。例如,
myService.AuthenticateUser(User).then(function (pl) {
console.log(pl.data)
if(pl.data){
$scope.msg = "Password or username is correct !"
}
else{
...
}
答
,如果你发布到你的HTTP POST请求的响应,这将有助于。我敢打赌,即使有不正确的登录信息,您仍然会提出有效的http请求,因此会得到一个有效的http响应(数据为pl.data),可能表明登录信息不正确。
如果pl.data为null或未定义,则只会触击else语句。研究JavaScript条件以及它们如何与对象一起工作。如果对象存在(不为空或未定义),则将其视为true。
如果尝试(pl.data = “”!) –
对不起仍创下if语句 – Mohammad
空输入fileld其命中else语句 - – Mohammad