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

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

    In order to get the http status code returned from the server, you can add validateStatus: status => true to axios options:

    axios({
        method: 'POST',
        url: 'http://localhost:3001/users/login',
        data: { username, password },
        validateStatus: () => true
    }).then(res => {
        console.log(res.status);
    });
    

    This way, every http response resolves the promise returned from axios.

    https://github.com/axios/axios#handling-errors

提交回复
热议问题