如何使用 axios 请求在服务器上捕获 404 错误?

How can I catch 404 error on server with axios request?

在 vue/cli 4 / vuejs 2.6.10 / vuex,3.0.1 应用程序中,我发出 axios 请求,当我收到 404 错误时 从服务器我想抓住它,比如:

this.credentialsConfig.headers.Authorization = 'Bearer ' + this.currentLoggedUserToken
axios.get(this.apiUrl + '/adminarea/categories/' + this.category_id, this.credentialsConfig)
    .then(({ data }) => {
        this.categoryRow = data.categories[0]
        this.is_page_loaded = true
    })
    .catch(error => {
        console.log('error::')
        console.log(error)
        console.log('error.status::')
        console.log(error.status) // NO VALUE

        debugger
        if (error.status == 404) { // tHAT DOES NOT WORK
            this.showPopupMessage("Categories editor", "User # "+this.category_id+" not found !", 'warn');
            this.$router.push({path: '/admin/categories'}); // TO MOVE TO OTHER ROUT!
        }

        console.error(error)
        this.showPopupMessage('Categories Editor', error.response.data.message, 'warn')
        this.is_page_loaded = true
    })

但是检查错误 var 我没有看到任何错误代码的结构:https://imgur.com/a/1qo527Z

如何在此处获取错误代码?

"axios": "^0.19.0",
"vue": "^2.6.10",
"vue-avatar": "^2.1.8",
"vuex": "^3.0.1"

谢谢!

使用 error.response.status 而不是 error.status。试试这个。如果这不合适,您可以使用 console.dir(error) 来显示错误对象。