search

Home  >  Q&A  >  body text

Vue.js axios interceptor responds 401

I get this error on the console: Uncaught (in promise) TypeError: Cannot read property of undefined at eval (read 'message'). This is my code in axios.js:

axiosIns.interceptors.response.use(
response => {
    if (response.message === 'Unauthenticated') {
        window.location = '/login'
    }

    return response
},
error => {
    if (error.response.message === 'Unauthenticated') {
        window.location = '/login'
    } else if (error.response.status === 401) {
        removeUserData()
        return Promise.reject(error)
    }
})

P粉066725148P粉066725148272 days ago433

reply all(1)I'll reply

  • P粉345302753

    P粉3453027532024-03-28 11:31:44

    Try replacing response.message and error.response.message with response.statusText and error.response.statusText.

    reply
    0
  • Cancelreply