因此,我正在尝试多种方法从axios HTTP调用中获取错误响应状态,但奇怪的事情正在发生。
getData() {
axios.get(`/api/article/getObserved.php`, axiosConfig)
.then(response => {
console.log('success');
console.log(response);
})
.catch(err => {
console.log('error');
console.log(err.status);
console.log(err.response.status)
});
}
所以我调用我的getObserved端点,尽管它正在返回
http_response_code(503);
它会
.then()
部分原因是it控制台日志“success”字符串。
这是控制台的输出:
获取
http://localhost/obiezaca/v2/api/article/getObserved.php
503(服务不可用)
成功收藏夹文章。vue?31bd:83
我打过几百个这样的电话
.catch
总是捕捉错误,即使我不会像其他语言那样抛出异常。但我也试过这样做:
getData() {
axios.get(`/api/article/getObserved.php`, axiosConfig)
.then(response => {
console.log('success');
console.log(response);
}, function (err) {
console.log('error');
console.log(err.status);
console.log(err.response.status);
})
.catch(err => {
console.log('error');
console.log(err.status);
console.log(err.response.status)
});
}
尽管我从端点返回了这个503错误请求,但它仍然不能处理“error”。为什么?
我还想补充一点,我不认为
my endpoint
工作不正常,因为我用cURL和POSTMAN手动测试了它,一切都很好。
编辑
由于当我不从端点获取数据时,响应是未定义的,并且我只需要处理一个错误(是否有数据),所以我只做了如下操作:
getData() {
axios.get(`/api/article/getObserved.php`, axiosConfig)
.then(response => {
if(response) {
this.articles = response.data.records;
} else {
this.noFavourite = true;
this.articles = [];
}
});
它正在发挥作用。我会祈祷不要在打电话时遇到同样的问题,因为我需要处理几个不同的错误。