简体   繁体   English

我在 JSON.parse 的位置 0 处不断收到 JSON 中的错误意外令牌 b?

[英]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.

相关问题 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 具有JSON.parse的JSON中位置0处的意外令牌&lt; - Unexpected token < in JSON at position 0 with JSON.parse JSON 中的意外令牌 a 在 position 0 在 JSON.parse - unexpected token a in JSON at position 0 at JSON.parse JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 从asyncStorage获取时的JSON.parse(object)引发错误:位置1的JSON中的意外令牌o - JSON.parse(object) when getting from asyncStorage throws error: Unexpected token o in JSON at position 1 使用JSON.parse时,出现“ SyntaxError:JSON中位置1的意外令牌” - I am getting “SyntaxError: Unexpected token ' in JSON at position 1” when using JSON.parse JSON. 解析 position 0 处的意外令牌 - JSON.parse unexpected token s at position 0 我不断收到此错误SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符 - I keep getting this error SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data json.parse意外令牌错误 - json.parse unexpected token error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM