How to catch 401 error using fetch method of javascript

前端 未结 5 858
野性不改
野性不改 2021-02-12 10:09

I need to catch error 401 Code of response so that I can retry after getting a new token from token endpoint. I am using fetch method get data from API.

   const         


        
5条回答
  •  盖世英雄少女心
    2021-02-12 10:58

    You can try this

    fetch(request)
      .then(function(response) {
        if (response.status === 401) {
          // do what you need to do here
        }
      })
      .catch(function(error) {
            console.log('DO WHAT YOU WANT')
    });
    

提交回复
热议问题