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

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

    What you see is the string returned by the toString method of the error object. (error is not a string.)

    If a response has been received from the server, the error object will contain the response property:

    axios.get('/foo')
      .catch(function (error) {
        if (error.response) {
          console.log(error.response.data);
          console.log(error.response.status);
          console.log(error.response.headers);
        }
      });
    

提交回复
热议问题