【发布时间】:2018-06-22 09:28:00
【问题描述】:
我的fetch 记录控制台错误,尽管已通过401 Unauthorized 解决
componentWillMount() {
this.setState({ isInitializing: true });
fetch('/api/users/getcurrent')
.then(resp => {
console.log('resolved');
if (resp.status == 200) {
// do stuff
}
this.setState({ isInitializing: false });
})
.catch(err => {
console.log('catched');
this.setState({ isInitializing: false });
});
}
这是 fetch 的预期行为吗?我没有看到任何与在 MDN 或其他任何地方的 401 上引发错误相关的内容。控制台记录是一个合法错误。我没有为fetch 使用任何填充物包装。 Chrome 版本为 63.0.3239.108。
【问题讨论】:
-
错误来自服务器。预期行为会在控制台中显示错误。
-
Chrome 在控制台中显示失败的请求。我相信非 200 或 300 会被认为是失败的。看起来
fetch确实 not 拒绝失败的请求。检查状态码由您决定。以下是fetch拒绝的一些原因:github.com/github/fetch/issues/201#issuecomment-308213104
标签: javascript promise fetch-api