vue-resource icon indicating copy to clipboard operation
vue-resource copied to clipboard

Can't handle the error callback in this.$http.post

Open Kuchiriel opened this issue 7 years ago • 4 comments

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.

Kuchiriel avatar Apr 19 '17 17:04 Kuchiriel

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 avatar Apr 23 '17 23:04 Kuchiriel

@Kuchiriel have you managed to get this one working? I have the same issue.

commercial-hippie avatar Aug 02 '17 20:08 commercial-hippie

@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 avatar Aug 05 '17 00:08 Kuchiriel

@Kuchiriel interesting, okay thanks for that!

commercial-hippie avatar Aug 05 '17 06:08 commercial-hippie