[英]I keep getting the error unexpected token b in JSON at position 0 at JSON.parse?
So I got this code that creates a html page.The function signup allows the user to register and create a password.所以我得到了这个创建一个html页面的代码。功能注册允许用户注册和创建密码。 The function checkpassword is to check if the correct password is entered for the username.It seems I have a problem in getting the item from local storage in my checkPassword function?Help will be much appreciated as I've been stuck for hours?
函数 checkpassword 用于检查是否为用户名输入了正确的密码。似乎我在 checkPassword 函数中从本地存储中获取项目时遇到了问题?如果我被困了几个小时,将不胜感激?
const PREFIX = "monash.eng1003.passwordApp.";
function checkPassword() {
var user = document.getElementById("registerUsername").value;
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var passwordToCheck = localStorage.getItem(PREFIX + user);
var passwordTwo = JSON.parse(passwordToCheck);
if (password != passwordTwo) {
alert("Don't hack" + user);
} else {
alert("Welcome" + user);
}
}
function signup() {
var user = document.getElementById("registerUsername").value;
var pass1 = document.getElementById("registerPassword").value;
var pass2 = document.getElementById("confirmPassword").value;
if ((pass1 === pass2) && (pass1 !== "")) {
if (localStorage) {
var passwordToStore = pass1;
localStorage.setItem(PREFIX + user, passwordToStore);
alert("Account created for username: " + user);
}
} else {
alert("Passwords must match and cannot be empty.")
}
}
EDIT:Thanks for pointing out that I do not need to parse it since I didn't stringify.That solved the problem but since I cannot delete the post I have to leave it here编辑:感谢您指出我不需要解析它,因为我没有字符串化。这解决了问题,但由于我无法删除帖子,我必须把它留在这里
You didn't convert the password to JSON when you stored it, so you don't need to use JSON.parse()
when you retrieve it.您在存储密码时没有将其转换为 JSON,因此您在检索它时不需要使用
JSON.parse()
。 You stored an ordinary string, you can just retrieve it and use it.您存储了一个普通字符串,您可以检索它并使用它。
passwordTwo = localStorage.getItem(PREFIX + user);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.