我不知道如何解决此错误:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我不知道如何解决此错误:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符相关的知识,希望对你有一定的参考价值。
我使用nodeJS进行表达。我不知道如何解决此错误,请帮忙:)在服务器中:
app.get("/home", (req, res) =>
userInfos = bll.retrieveDataUser(); // [userEmail, userPassword, userName, userId]
res.send(
userEmail: userInfos[0],
userPassword: userInfos[1],
username: userInfos[2],
uId: userInfos[3]
);
在客户端:
fetch(`/home`).then((res) =>
res.json().then((data) =>
if (data.error)
throw error
else
console.log(data);
)
)
感谢您的帮助:)
答案
如果要返回json,则应使用express的res.json
方法。
app.get("/home", (req, res) =>
userInfos = bll.retrieveDataUser(); // [userEmail, userPassword, userName, userId]
res.json(
userEmail: userInfos[0],
userPassword: userInfos[1],
username: userInfos[2],
uId: userInfos[3]
);
);
并且您的提取请求语法也无效。
fetch(`/home`)
.then(res => res.json())
.then((data) =>
if (data.error)
throw error
else
console.log(data);
)
以上是关于我不知道如何解决此错误:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符的主要内容,如果未能解决你的问题,请参考以下文章