【发布时间】:2020-10-11 20:32:18
【问题描述】:
我正在使用 Axios 发出请求,并且一切正常,但是如果 PHP API 中出现错误,例如:“未定义索引:...”。 Axios 不处理 'Content-Type': 'application/json' 的错误
const API = axios.create({
baseURL: URL + "/api/",
headers: {
'Content-Type': 'application/json',
}
})
let data = {
orderList: {
limit: 10,
page: page,
orderby: orderBy,
search: search
}
};
data = qs.stringify(data);
let orderList;
await API.post("api.php", data, {
headers: {
'Content-Type': 'application/json',
}
}).then(response => {
orderList = response.data;
}).catch(() => {
orderList = 'Network Error';
})
return orderList;
},
我希望它只返回来自 API 的 JSON 结果,如果没有,则触发 catch。但是 catch 没有被触发。
【问题讨论】:
-
headers: { 'Accept': 'application/json', } 不起作用@LawrenceCherone
-
您返回的响应的 HTTP 状态码是什么? 200? 500?
-
@ceejayoz 200。详细信息:我在 PHP 中触发错误,我在终端反应本机日志中看到它
-
那就是为什么。 200 状态不会触发
catch。这不是错误; Axios 会认为它是成功的。如果您将有关错误的 JSON 数据作为 200 个响应返回,则必须在then块中处理它们。
标签: php react-native axios