我在 JSON 文件中有两组数据:
Admins = [{"Admin":"jhonmorris"},{"Admin":"clarkechris"}]
UIDS = [{"password":"Jhon", "username":"jhonmorris"}, {"password":"Jean", "username":"jeanheadly"}, {"password":"Clarke", "username":"clarkechris"}]
我有 HTML ,用户登录页面为:
<html>
<head>
<script type="text/javascript" src="data.json" ></script>
<script type="text/javascript" src="checking.js" ></script>
</head>
<body>
<form name="myForm" method="post">
UserName<input type="text" id="uid" value="uid"/>
UserPassword<input type="password" id="pwd" value="pwd"/>
<input type="submit" value="Submit" onclick="fnCheck()"/>
</form>
</body>
</html>
我有两种类型的用户,一种是admin
( JSON 中的Admins
),另一种是regular user
(除了任何其他用户的用户名)来自 JSON 中的Admins
。单击提交按钮后,我想在var x
中输入用户ID,并检查它是否具有与 JSON 中的UIDS
对应的正确密码。此外,我想检查相同的用户名是否属于 JSON 中的Admins
数据集。如果是,我想将他重定向到谷歌。如果没有,我想将他重定向到雅虎。
我正在使用 JavaScript 阅读 JSON 数据,如下所示:
function fnCheck(){
alert("in fnCheck()");
if(checkUP() == true){
alert("checkUP returned true! UserID and Password are correct");
if(checkAdmin() == true){
alert("checkAdmin() returned true! going to admin page now");
window.location = 'http://www.google.com';
}
else if(checkAdmin() == false){
alert("checkAdmin() returned false! going to userIndex page now");
window.location = 'http://www.yahoo.com';
}
}
else{
alert("checkUP() returned false. Creds are Invalid!");
}
}
function checkAdmin(){
alert("in checkAdmin()");
//var adminJSON = JSON.parse(Admins);
//length = adminJSON.length;
x = document.forms["myForm"]["uid"].value;
for(var key in Admins){
var admin = Admins[key]
//alert(""+admin.Admin)
if(admin.Admin == x){
alert("Admin match found. Returning true!");
return true;
}
else{
alert("Admin match not found. Returning false");
return false;
}
}
}
function checkUP(){
alert("hello!!");
var x = document.forms["myForm"]["uid"].value;
var y = document.forms["myForm"]["pwd"].value;
alert("In checkUP, value of x = "+x+" calue of y = "+y);
for(var key in UP){
var user = UP[key]
//if(user.username == x && user.password == y){
if(x.match(user.username) && x.match(user.password)){
alert("user.username = "+user.username+"user.password = "+user.password+" User Id and password matched. Returning true now");
//alert("Matching "+x+ "with "+user.username"+ and "+y+" with user.password"+ y);
//break;
return true;
}
else{
//alert("User ID and password did not match. Returning false now. user.username = "+user.username+" user.password = "+user.password);
//break;
return false;
}
}
}
我尝试过发出警报,找出所有地方出了什么问题,但我仍然无法弄清楚出了什么问题。非常感谢帮助!
答案 0 :(得分:1)
此处未调用您的函数,请勿使用提交按钮的onclick
事件,更好地使用onsubmit
形式的onsubmit="fnCheck()"
事件:
<form name="myForm" method="post" onsubmit="fnCheck(event)">
注意:强>
在JSON中存储密码是一种非常糟糕的做法,会使您的网页非常虚弱和不安全。
给定文件不提供有效的JSON结构,这些只是JavaScript对象,假设您有一个有效的JSON,请使用JSON.parse(json)
来解析您的json对象/数组。
undefined
变量,例如UP
。onsubmit
测试是否正确调用了您的函数,但仍需要删除/更正undefined
个变量。
var Admins = [{
"Admin": "jhonmorris"
}, {
"Admin": "clarkechris"
}];
UIDS = [{
"password": "Jhon",
"username": "jhonmorris"
}, {
"password": "Jean",
"username": "jeanheadly"
}, {
"password": "Clarke",
"username": "clarkechris"
}];
function fnCheck(e) {
e.preventDefault();
console.log("in fnCheck()");
if (checkUP() == true) {
console.log("checkUP returned true! UserID and Password are correct");
if (checkAdmin() == true) {
alert("checkAdmin() returned true! going to admin page now");
window.location = 'http://www.google.com';
} else if (checkAdmin() == false) {
alert("checkAdmin() returned false! going to userIndex page now");
window.location = 'http://www.yahoo.com';
}
} else {
alert("checkUP() returned false. Creds are Invalid!");
}
}
function checkAdmin() {
alert("in checkAdmin()");
//var adminJSON = JSON.parse(Admins);
//length = adminJSON.length;
x = document.forms["myForm"]["uid"].value;
for (var key in Admins) {
var admin = Admins[key]
//alert(""+admin.Admin)
if (admin.Admin == x) {
console.log("Admin match found. Returning true!");
return true;
} else {
console.log("Admin match not found. Returning false");
return false;
}
}
}
function checkUP() {
console.log("hello!!");
var x = document.forms["myForm"]["uid"].value;
var y = document.forms["myForm"]["pwd"].value;
console.log("In checkUP, value of x = " + x + " calue of y = " + y);
for (var key in UP) {
var user = UP[key]
//if(user.username == x && user.password == y){
if (x.match(user.username) && x.match(user.password)) {
console.log("user.username = " + user.username + "user.password = " + user.password + " User Id and password matched. Returning true now");
return true;
} else {
return false;
}
}
}
<form name="myForm" method="post" onsubmit="fnCheck(event)">
UserName
<input type="text" id="uid" value="uid" />UserPassword
<input type="password" id="pwd" value="pwd" />
<input type="submit" value="Submit" />
</form>
修改强>
在你的函数中,你在if语句中返回true / false,所以它总是在第一次迭代中离开函数。
您应该使用布尔标志并在if语句中相应地更改其值,并仅在函数末尾返回它:
function checkUP() {
console.log("hello!!");
var bool = false;
var x = document.forms["myForm"]["uid"].value;
var y = document.forms["myForm"]["pwd"].value;
console.log("In checkUP, value of x = " + x + " calue of y = " + y);
for (var key in UP) {
var user = UP[key]
if (x === user.username && y === user.password) {
console.log("user.username = " + user.username + "user.password = " + user.password + " User Id and password matched. Returning true now");
return true;
}
}
return bool;
}