3

i have code something similar like this

  fetch("http://httpstat.us/401",   {
    method: 'GET',
    headers: {
       "Accept": "application/json",
       "Accept-Language": "en-US",
       "Content-Type": "application/json",
       "token": localStorage.getItem('access_token')
        }            
    }
  ).then(response =>{
    if(response.ok){
      return Promise.resolve(response.json())
    }
    else {
        return Promise.reject();
      }
    }
 }
)

so in my headers if i wont send the token or say localstorage is not having access_token or it is empty then from server 401 error is thrown in the console, can any body please give me a way so that i can handle the error and it is not thrown in the console

And i don't want a solution like this because it is a simple hack which programmer dose it for his satisfaction , how can we tell the client to do all this kind of stuff. so i want a programmatic solution to avoid this.

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
Shiva Sai
  • 463
  • 4
  • 12
  • 1
    `fetch(url, options).then(/* handle success */).catch(/* handle error */)` – Ray Apr 19 '18 at 08:30
  • @Ray, Error in terms of *fetch* is that the response was not received. Otherwise the response is received successfully with its status whether it was 200, 403, or 500 etc. – Ali Ankarali Apr 19 '18 at 08:34
  • exactly @AliAnkarali and i am getting the 4xx error in the console, how to stop it, even though i am handling in my code for unsuccessful response – Shiva Sai Apr 19 '18 at 08:37

0 Answers0