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

前端 未结 10 1949
南旧
南旧 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:54

    As @Nick said, the results you see when you console.log a JavaScript Error object depend on the exact implementation of console.log, which varies and (imo) makes checking errors incredibly annoying.

    If you'd like to see the full Error object and all the information it carries bypassing the toString() method, you could just use JSON.stringify:

    axios.get('/foo')
      .catch(function (error) {
        console.log(JSON.stringify(error))
      });
    

提交回复
热议问题