【发布时间】:2019-06-20 14:19:48
【问题描述】:
我有 Python Flask (v 1.0.0) 后端,它返回状态为 400 的 http 响应和自定义错误消息
return Response('My custom errror message', 400)
在我的前端,我使用 Fetch API 对后端进行 API 调用:
fetch(url, options)
.then(response => response.json())
.then(response => console.log(response))
.catch(error => console.log(error));
由于 400 是 BAD REQUEST,fetch 直接转到 catch,而我只有 TypeError: Failed to fetch 消息。
如何在前端获取自定义错误消息而不是 Failed to fetch 消息?它在 DevTools 中可见,所以我希望它应该以某种方式在 fetch 中可用?
【问题讨论】:
-
您是否尝试过访问
then块中的响应代码? -
它直接转到
.catch,我认为是因为它是 400 响应,fetch立即将其视为错误。 -
试试这个 github.com/github/fetch/issues/203#issuecomment-143347675
标签: javascript http fetch-api