我正在开发一个课堂测验应用程序,我一直在控制台中收到错误“Uncaught SyntaxError: Unexpected token o in JSON at position 1”。我曾尝试在 YouTube 视频中查找解决方案,甚至尝试通过本网站上的类似帖子修复代码。我不知所措,不知道出了什么问题。
const username = document.querySelector('#username');
const saveScoreBtn = document.querySelector('#saveScoreBtn');
const finalScore = document.querySelector('#finalScore');
const mostRecentScore = localStorage.getItem('mostRecentScore');
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
const MAX_HIGH_SCORES = 5;
finalScore.innerText = mostRecentScore;
username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
});
saveHighScore = e => {
e.preventDefault();
const score = {
score: mostRecentScore,
name: username.value
};
highScores.push(score);
highScores.sort((a,b) =>{
return b.score - a.score;
})
highScores.splice(5);
localStorage.setItem('highscores', JSON.stringify(highScores));
window.location.assign('../high-scores/highscores.html')
};
答案 0 :(得分:-1)
“Uncaught SyntaxError: Unexpected token o in JSON at position 1”
这意味着这不是 json 格式的代码。再次检查您的 json。