I want to catch PHP response errors with Axios.
I have a simple PHP code like this to try to send error to my js file:
header('Content-type: application/json; charset=utf-8');
$test = array (
'error' => 'test error!'
);
echo json_encode($test);
die();
And my js with axios like this:
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?
}
)
I can't show error inside catch
I think because I made echo json_encode
and Axios can not differenciate between error and normal response.
Is there some way to catch errors inside catch
?