【发布时间】:2025-12-29 20:45:06
【问题描述】:
“JSON 解析错误:无法识别的令牌'
点击 API 时出现错误。 (响应采用 JSON 格式。)我正在尝试构建登录表单,但无法从数据库服务器检索用户数据。
constructor(){
super()
this.state={
email:'',
password:'',
}
}
handleLoginUser(){
fetch('https://"mygoogleclouddatabaseip"/users/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(this.state)})
.then(response => {
console.log(response);
return response.json();})
.then(responseData => {
console.log(responseData);
let result = JSON.parse(responseData);
return result;})
.then(data => {
if (result.success){
alert("Login successful");
}
else
alert("Unable to Login");
})
}
}
【问题讨论】:
-
检查服务器的响应。听起来服务器可能正在返回 HTML 或其他一些非 JSON 响应。
-
"响应是 JSON 格式" — 也许应该是这样,但错误消息另有说明。
-
response.json()和let result = JSON.parse(responseData);?真的吗?你确定要这样做吗?
标签: javascript reactjs react-native