gpt4 book ai didi

javascript - 为什么我总是从 Chrome 获得 "Uncaught SyntaxError: Unexpected token u "?

转载 作者:可可西里 更新时间:2023-11-01 01:31:49 24 4
gpt4 key购买 nike

我为此花了一整天的时间,在谷歌上搜索并寻找答案,但仍然无法弄清楚。

我的代码有点长,它在 Firefox 中运行良好,但从 Chrome 获取“Uncaught SyntaxError: Unexpected token u”。

谁能指出我错在哪里?提前致谢!

// when the page loads, list all the current contacts
$(document).ready(function(){

// check if localStorage database exists
if(!localStorage.getItem("customerDatabase")){

// define a JSON object to hold all current address
var contacts = {
"users":[
{
"id":"1",
"name":"dennis",
"email":"dennisboys@gmail.com"
},
{
"id":"2",
"name":"zoe",
"email":"zoeisfemale@gmail.com"
}
]
} // end of contacts JSON object

// stringify the object
var stringObject = JSON.stringify(contacts);

// store it into localStorage database
var storedDatabase = localStorage.setItem("customerDatabase", stringObject);

} else {
// list all customers upon page loads
listJSONCustomers();
}

// list all current contacts from JSON object in localStorage
function listJSONCustomers(){

var displayHTML = "";
var i;

// get the data from localStorage
var storedDatabase = localStorage.getItem("customerDatabase");

// parse the data from string to JSON object
var parseObject = JSON.parse(storedDatabase);

// access the users key of the JSON object
var userObject = parseObject.users;

// get the length of the object (how many customers the database has)
var contactsLength = userObject.length;

for(i=0; i<contactsLength; i++){
var trElement = '<tr id="address' + (i+1) + '">';
var tdId = '<td id="id' + (i+1) + '">' + userObject[i].id + '</td>';
var tdName = '<td id="name' + (i+1) + '">' + userObject[i].name + '</td>';
var tdEmail = '<td id="email' + (i+1) + '">' + userObject[i].email + '</td>';
var tdButton = '<td id="button"><button id="editButton' + userObject[i].id + '">Edit</button> | <button id="deleteButton' + userObject[i].id + '">Delete</button></td>';

displayHTML += trElement + tdId + tdName + tdEmail + tdButton + '</tr>';
}

$('#address_list').html(displayHTML);
}

// add customer to database
$('#saveCustomer').click(function(){

if( $('#customerName').val() !== "" && $('#customerEmail').val() !== "" ){

var customerName = $('#customerName').val();
var customerEmail = $('#customerEmail').val();

// get the data from localStorage
var storedDatabase = localStorage.getItem("customerDatabase");

// parse the data from string to JSON object
var parseObject = JSON.parse(storedDatabase);

// access the users key of the JSON object
var userObject = parseObject.users;

// get the new entry
var newCustomerObject = {
"id": userObject.length + 1,
"name": customerName,
"email": customerEmail
};

// push the new entry into the object
userObject.push(newCustomerObject);

// convert the object into string for localStorage
var stringObject = JSON.stringify(parseObject);

// store the JSON object into localStorage
var storedDatabase = localStorage.setItem("customerDatabase", stringObject);

// list all customes again every time a database receives a new entry
listJSONCustomers();

} else {
alert("Please enter customer's name and email.");
}

}); // end of $('#saveCustomer').click();


});

最佳答案

在某些时候,您所做的某些事情损坏了该键的 LocalStorage 值。 LocalStorage 只能存储字符串,因此如果您向它传递任何其他内容,它会将其转换为字符串。由于您的值为 'undefined',这意味着在某些时候,您可能不小心做了这样的事情:

var value;
localStorage.setItem('key', value);

在这种情况下,valueundefined,它不是字符串。保存后,它将被转换。不幸的是,"undefined" 不是有效的 JSON。这意味着当它尝试解析时,它会抛出异常。

要解决您的问题,您应该使用 removeItem 清除错误值。

localStorage.removeItem("customerDatabase");

关于javascript - 为什么我总是从 Chrome 获得 "Uncaught SyntaxError: Unexpected token u "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14681366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com