簡體   English   中英

如何讀取JSON數據並將其與變量進行比較

[英]How to read JSON data and compare it with a variable

我在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>

我有兩種類型的用戶,一種是adminAdminsJSON中 ),另一種是regular user (除了JSON中的 Admins之外的任何其他用戶用戶名)。 單擊提交按鈕時,我想獲取在var x輸入的userID,並檢查它是否具有與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;
        }
    }
}

我試過發出警報,找出所有地方出了什么問題,但我仍然無法弄清楚出了什么問題。 非常感謝幫助!

這里不調用你的函數,不要使用提交按鈕的onclick事件,最好使用onsubmit="fnCheck()"形式的onsubmit事件:

<form name="myForm" method="post" onsubmit="fnCheck(event)">

注意:

  • 在JSON中存儲密碼是一種非常糟糕的做法,會使您的頁面非常脆弱和不安全。

  • 給定文件不提供有效的JSON結構,這些只是JavaScript對象,假設您有一個有效的JSON,使用JSON.parse(json)來解析您的json對象/數組。

  • 您的函數中存在一些錯誤,例如使用undefined變量,例如UP的情況。
  • 這是一個DEMO,您可以使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM