0

I'm trying to catch 401 error in the error callback of axios (I just want to detect that it's a 401 error) , but it doesn't has any attribute that indicate about 401 .

i.e -

test401() {
    var self = this;
    axios.post('https://mail.google.com/sync/u/0/i/fd?hl=en&c=12')
    .catch( (error) => {
        self.setState({data: error.status || "No Status" });
      console.log("catch ", error); // No any status here
    });
}

Here is the jsfiddler if you want to check it

How could I know in axios that I got 401 error response ?

Kyle Dormer
  • 39
  • 10
URL87
  • 10,667
  • 35
  • 107
  • 174
  • It says "Access to XMLHttpRequest at 'https://mail.google.com/sync/u/0/i/fd?hl=en&c=12' from origin 'https://fiddle.jshell.net' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." — So you can't access the status of the reqest from JavaScript. – Quentin May 21 '19 at 13:33
  • It should be `error.response.status` – ig0774 May 21 '19 at 13:33
  • @Quentin , oh really ? even if I can see the 401 in the console ? – URL87 May 21 '19 at 13:34
  • "oh really ?" – Yes, the duplicate explains that – Quentin May 21 '19 at 13:35
  • "even if I can see the 401 in the console ?" — Yes, the duplicate explains why – Quentin May 21 '19 at 13:35

0 Answers0