How can I get the status code from an http error in Axios?

前端 未结 10 1944
南旧
南旧 2020-11-28 02:27

This may seem stupid, but I\'m trying to get the error data when a request fails in Axios.

axios.get(\'foo.com\')
    .then((response) => {})
    .catch((         


        
10条回答
  •  渐次进展
    2020-11-28 02:53

    You can use the spread operator (...) to force it into a new object like this:

    axios.get('foo.com')
        .then((response) => {})
        .catch((error) => {
            console.log({...error}) 
    })
    

    Be aware: this will not be an instance of Error.

提交回复
热议问题