Я хочу поймать ошибки PHP-ответа с помощью Axios.
У меня есть простой PHP-код, подобный этому, чтобы попытаться отправить ошибку в мой js-файл:
header('Content-type: application/json; charset=utf-8');
$test = array (
'error' => 'test error!'
);
echo json_encode($test);
die();
И мои js сВот такие аксиозы:
axios({
method: 'post',
responseType: 'json',
url: 'test.php'
data: '1234'
})
.then((response) => {
if ( response.data.error ) {
console.log(response.data.error) // I show error here
}
else {
console.log(response.data);
}
})
.catch((error) => {
console.log (error); // it's possible to show error here?
}
)
Я не могу показать ошибку внутри catch
Я думаю, потому что я сделал echo json_encode
, а Axios не может различить ошибку и нормальный ответ.
Есть ликакой-то способ отловить ошибки внутри catch
?