vue-resource
vue-resource copied to clipboard
Can't handle the error callback in this.$http.post
I receive a valid response from success callback, but from the error callback I don't receive any valid response that I can handle.
I am using it on latest Chromium with vue-admin.
Code: https://jsfiddle.net/cbgjvmxb/
Thanks.
I discovered the problem, when the backend returns a status code that is considered an error, such as 400, a problem occurs. But if I use a http code such as 200, I can get the object with the status code and the message.
@Kuchiriel have you managed to get this one working? I have the same issue.
@commercial-hippie I still using status code 200, and I create a separate function to handle both response and error, giving priority to the positive status code.
addUser: function () {
this.axios.post(`${defaultURL}/v1/register`, {
email: toLower(this.email),
nome: toLower(this.nome),
perfil: toLower(this.perfil),
unidade: toLower(this.unidade),
senha: this.senha
}).then(res => {
this.notify(res)
this.close()
}).catch(err => {
this.notify(err)
})
},
Example: notify: function (res) {
if (res.status === 201) {
openNotification({
title: 'Gerenciamento',
message: res.data.message,
type: 'success'
})
} else {
console.log(res)
openNotification({
title: 'Erro',
message: `${res.message},
reporte para os desenvolvedores da aplicação`,
type: 'danger'
})
}
},
@Kuchiriel interesting, okay thanks for that!