Angular JS应用程序Script.JS归档返回错误消息
问题描述:
我正在将Wcf服务用于Angular JS应用程序。我正在通过提供用户名和密码来创建用户登录系统。但是,我在输入中输入的任何用户名或密码都会提示其始终显示消息用户名和密码是否正确。我想如果用户名和密码是正确的,那么我想显示消息在角js应用程序,否则用户名和密码是不正确的,但我不知道为什么我得到同样的消息时,我运行应用程序..Angular JS应用程序Script.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) {
$scope.msg = "Username and password is correct ";//Always hit on this line
}, function (err) {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
});
};
}]);
app.service("myService", function ($http) {
// Create new record
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>
答
假设你的Ajax响应是文本 '真' 或 '假'(正如你所提到的),这里是代码:
$scope.login = function() {
var User = {
Username: $scope.Username,
Password: $scope.Password,
};
myService.AuthenticateUser(User).then(function (pl) {
if(pl.data){
$scope.msg = "Username and password is correct ";//Always hit on this line
}
else
{
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
}
}, function (err) {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
});
};
登录不正确时,您的服务是否发送错误代码?这可能是它发送给你一个不同的成功代码,或不同的味精...但请求仍然成功,所以你的味精总是成功的。在浏览器中观察Dev Tools的Networking Tab中的响应。 – dev8080
在wcf服务中,它是布尔方法,它只返回true或false。当我输入错误的用户名或密码时,它返回false,但在Angular js应用程序中始终显示消息用户名和密码是正确的。它从来没有命中函数的错误方法 – Mohammad
使用pl.data中返回的内容 – dev8080