javascript 'Uncaught SyntaxError: Unexpected token u' 使用 JSON.parse 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20173372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:56:17  来源:igfitidea点击:

'Uncaught SyntaxError: Unexpected token u' when using JSON.parse

javascriptjqueryjson

提问by Juvi

I'm using JSON.parse as a simple database on my computer with LocalStorage. It works smoothly until I'm doing the check of this "database"; heres the code for entering information to LocalStorage:

我使用 JSON.parse 作为我计算机上带有 LocalStorage 的简单数据库。它运行顺利,直到我检查这个“数据库”;下面是将信息输入到 LocalStorage 的代码:

var users = JSON.parse(localStorage.registeredUsers);
users.push({username:name, password:userpass, connected:false});
localStorage.registeredUsers = JSON.stringify(users);

and when I', having the check of that registeredusers I get the error "Uncaught SyntaxError: Unexpected token u":

当我检查注册用户时,我收到错误“Uncaught SyntaxError: Unexpected token u”:

var users = JSON.parse(localStorage.registeredUsers);
        if(users[userindex].connected)
        {.........}

The error points to the line with JSON.parse. I tried to figure it out with some similiar topics but couldnt find the way.

错误指向带有 JSON.parse 的那一行。我试图用一些类似的主题来解决它,但找不到方法。

The code that I push into the array of localstorage:

我推入 localstorage 数组的代码:

function regBtn(event)
    {
        event.preventDefault();
        name=document.forms["regform"]["username"].value;
        userpass=document.forms["regform"]["password"].value;
        localStorage.username=name;
        localStorage.password=userpass;
        if(!(localStorage.registeredUsers))
        {
            localStorage.registeredUsers = '[]';
        }
        var users = JSON.parse(localStorage.registeredUsers);
        users.push({username:name, password:userpass, connected:false});
        localStorage.registeredUsers = JSON.stringify(users);
        $('#mainContent').load('HomePage.html');        
    }

采纳答案by Charlie Affumigato

Try

尝试

var users = localStorage.registeredUsers? JSON.parse(localStorage.registeredUsers) : [];

or if you don't like the ternary operator,

或者如果你不喜欢三元运算符,

var users=[];
if(localStorage.registeredUsers){
  users=JSON.parse(localStorage.registeredUsers);
}

might help

可能有帮助

回答by bikshu

function setCookies(){
        var cookiesObjects = {};
        cookiesObjects.name = document.getElementById('usr').value;
        cookiesObjects.email = document.getElementById('email').value;
        cookiesObjects.age = document.getElementById('age').value;

        var actualString = JSON.stringify(cookiesObjects);


        document.cookie = "user="+ actualString;



    }
    function getCookies(){
        var nameValueArray = document.cookie.split("=");
        var cookieObject = JSON.parse(nameValueArray[1]);
      document.getElementById('usr').value=cookieObject.name;
      document.getElementById('email').value=cookieObject.email;

      document.getElementById('age').value=cookieObject.age;



    }
    function clearCookies(){
        document.getElementById('usr').value= "";
        document.getElementById('email').value= "";
        document.getElementById('age').value= "";
    }
and saying Error : Uncaught SyntaxError: Unexpected token u

I can't identify the error's cause.

我无法确定错误的原因。