0
我在構建Web應用程序並需要構建登錄站點。如何循環訪問此JavaScript對象,並將登錄表單上的用戶輸入(此時僅需要電子郵件(「EML」))與數據進行比較以確保輸入正確?我試圖做出一些事情,但只是想出了下面的for循環,這只是一團糟。 (該對象是在一個單獨的js文件中) 謝謝!如何通過對象循環並將結果與輸入進行比較
function validate() {
var un = document.login.username.value;
var pw = document.login.password.value;
var valid = false;
for (var key in responseData) {
if (responseData.hasOwnProperty(key)) {
for (var i = 0; i < key.length; i++) {
if (un == key[i]) {
valid = true;
break;
}
}
}
if (valid) {
alert("Login was successful. Welcome, " + un + ".")
window.location = "https://www.google.com";
return false;
}
}
var responseData = {
authenticatUser: {
"ERR":0,
"RSP":{
"AUTHC":"true",
"USR":{
"ID":"2",
"TJT":"FULL",
"ACTV":"true",
"BO":"1489760664786",
"CONT":{
"FNM":"John",
"LNM":"Doe",
"PHN":"5556667777",
"PHNTP":"NONE",
"EML":"[email protected]",
"EMLTP":"NONE"
},
"ADMIN":"false",
"LLOGN":"1489760664786",
"ACCT":{
"ID":"2",
"TJT":"ID"
}
}
}
},
getUserAccountDetails: {
"ERR":0,
"RSP":{
"ACCT":{
"ID":"2",
"TJT":"FULL",
"ACTV":"true",
"BO":"1489760664786",
"LU":"1489760664786",
"NM":"Name",
"DESC":"Description",
"CONT":{
"FNM":"John",
"LNM":"Doe",
"PHN":"5556667777",
"PHNTP":"NONE",
"EML":"[email protected]",
"EMLTP":"NONE"
},
"ADDRM":{
"STRT":"1 Miracle Way",
"CITY":"San Antonio",
"STATE":"Texas",
"ZIP":"78245"
},
"ADDRB":{
"STRT":"1 Miracle Way",
"CITY":"San Antonio",
"STATE":"Texas",
"ZIP":"78245"
},
"TZ":"US_CT",
"LICS":"1",
"REPOS":[
{
"ID":"2",
"TJT":"ID"
},
{
}
],
"USRS":[
{
"ID":"2",
"TJT":"ID"
},
{
}
]
}
}
}
};
如果你問如何訪問js對象內的嵌套屬性,你會發現答案[在這裏](https://stackoverflow.com/questions/11922383/access-process-nested-objects-array -or-JSON)。還得警告你,存儲和訪問用戶信息的JSON並不是一個非常安全的方式來創建一個登錄頁面。 –