I have server with HTTP basic authentication and I have client script which knows username and password for above HTTP server. I use XHR
request and it's setRequestHeader
to set Authorization headers, this bit avoids browser from prompting default HTTP login dialog.
request.setRequestHeader('Authorization', authInfo);
This is normal for AJAX requests, but in case I want download file from mentioned server and I'm forced to go without AJAX and use something like window.location.href
from JavaScript where browser creates request itself. Since this request will not include Authorization header (brwoser will add Authorization header for every request automatically only after it prompts login dialog and saves base64 of login info for actual server) browser will prompt HTTP login dialog, which I want to avoid.
So is there a way to set headers to non-ajax requests created by browser?
Have you tried setting the username and password in the URL?
location.href = 'http://user:[email protected]/path/to/download';
I think that should work in most browsers except, unfortunately, Internet Explorer 7+.
Alternatively, if you have access to the server, you could put a script on the server side that uses cookies to authenticate. Cookies can be set with JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With