How to catch 401 error using fetch method of javascript

前端 未结 6 1115
离开以前
离开以前 2021-02-12 09:59

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         


        
6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 10:40

    You can check the status of the response in then:

    fetch(request)
      .then(function(response) {
        if (response.status === 401) {
          // do what you need to do here
        }
      })
      .catch(function(error) {});
    

提交回复
热议问题