2017-07-15 68 views
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" 
      }, 
      { 
      } 
     ] 
     } 
    } 
} 

};

+1

如果你問如何訪問js對象內的嵌套屬性,你會發現答案[在這裏](https://stackoverflow.com/questions/11922383/access-process-nested-objects-array -or-JSON)。還得警告你,存儲和訪問用戶信息的JSON並不是一個非常安全的方式來創建一個登錄頁面。 –

回答

0

首先,您不應該認證登錄詳細信息客戶端或以純文本保留客戶端詳細信息,我假設您知道這一點,但您只是在學習。

for (var key in responseData) { 
     // THIS IS REDUNDANT 
     // YOU GOT THE KEY FROM THE OBJECT SO THIS ISN'T 
     // NECESSARY 
     if (responseData.hasOwnProperty(key)) { 
      // ITERATING OVER THE LENGTH OF THE KEY 
      // DOESN'T MAKE A LOT OF SENSE 
      // You're not in fact iterating over the object 
      for (var i = 0; i < key.length; i++) { 
       if (un == key[i]) { 
        valid = true; 
        break; 
       } 
      } 
      } 

你想要做的是簡化了JSON響應數據的東西沿着

{ 
    "accounts" : [ 
     { 
     "id" : "xxxxxxx", 
     "username" : "xxxxxx", 
     "password" : "xxxxxx" 
     }, 
     { 
     "id" : "xxxxxxx", 
     "username" : "xxxxxx", 
     "password" : "xxxxxx" 
     }, 
     { 
     "id" : "xxxxxxx", 
     "username" : "xxxxxx", 
     "password" : "xxxxxx" 
     } 
    ] 
} 

該行,然後你會檢查用戶憑據,像這樣:

let data = JSON.parse(responseData); 
found = false; 
for(var account in data.accounts) 
{ 
    if(account.username === un && account.password === pw) 
    { 
     found = true; 
     break; 
    } 
} 

if(found) 
{ 
    //POST user id back to server to get details 
} 

請注意,我不建議以這種方式進行任何驗證。如果你想模擬好你應該運行一些簡單的apache php或節點服務器,它將接受用戶名和密碼並驗證服務器端並返回結果。

相關問題