Message from JavaScript discussions

October 2018

— Hello guys

— 

How can i detect via JS if a basic autentication went wrong? For example, can i detect with javascript if I am unable to load a file in a protected folder?

— Ajax?

— My server seems to respond with status 401 for wrong username / password

— You generally just check if status is 200-299 for OK

— Let me explain myself: I have an app and a file in a separate folder, protected via .htaccess. If I press a button to load the protected file, my server asks for username and password. If I cannot do the login I am unable to load the file... it is possible to detect with a script that I cannot load the protected file?

Message permanent page

— Yes

— How?

— But the script may cause the login to pop up again

— Ajax / xhr / fetch

— 

fetch('http://example.com/protected/path')
.then(response =>
response.ok
? alert('Access granted')
: alert('Access denied'));

Message permanent page

— Thank you very much