【发布时间】:2021-10-20 10:33:34
【问题描述】:
我有一个 React 应用程序设置为使用 Santum 调用 Laravel API,但我遇到了返回错误代码的 API 调用问题。然而,成功的结果工作正常。
例如,如果我运行:
axios.get('http://website.test/api/collections')
.then(function (response) {
console.log(JSON.stringify(response));
})
.catch(function (error) {
console.log(JSON.stringify(error));
});
然后我按预期返回一个集合列表,一切都很好。
但是,如果我尝试一些应该出错的东西(比如 404):
axios.get('http://website.test/api/doesnotexist')
.then(function (response) {
console.log(JSON.stringify(response));
})
.catch(function (error) {
console.log(JSON.stringify(error));
});
它总是调用'then'函数而不是catch,但内容是:
Error: Request failed with status code 404
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)
带有 422 响应的验证错误会发生非常相似的情况。
我已经按照文档设置了 Sanctum,并且 axios 使用 withCredentials。正如我所说,来自 API 的 200/201 响应工作正常,但我就是不明白为什么错误不能?控制台中也没有显示 CORS 错误。
【问题讨论】: