说我有一个意外的令牌<在位置0的JSON中。知道怎么了吗?可能很简单,但我还在学习。
> 29 | .then(response => response.json())
30 | .then(user => {
31 | if (user.id) {
32 | this.props.loadUser(user)
this.props.onRouteChange('home');
}
})
}
我过去常常在服务器返回错误时得到这个。例如,它不会返回json,而是返回一些html标记:
<!doctype html>
这个意外的符号可能是html标记的左括号。
响应不是JSON响应,很可能是错误代码范围为400或500的HTML错误页面。
看起来你正在使用fetch,所以你可以将该函数改为
.then(response => response.ok && response.json())
在假设它是JSON之前,这将检查它是否是一个好结果,但您可能应该添加一个.catch()
来捕捉这些时候它不是您期望的响应。
你很可能会在网络标签中查看错误,或者在你的捕获中做这样的事情:
.catch(response => response.text().then(errText => console.log(errText))