Pure JavaScript code for HTTP Basic Authentication?

JavascriptAjaxHttpBasic Authentication

Javascript Problem Overview


Where can I find reference code that implements a HTTP Basic Authentication client in pure JavaScript, suitable for AJAX?

Extra points for code, or pointers to code, that can be used independent of JS toolkits like YUI. No points for Java, Flash/Flex, PHP frameworks, etc.

Javascript Solutions


Solution 1 - Javascript

The five-parameter version of the XMLHttpRequest.open method allows you to specify the username and password. (WHATWG spec)

xhr.open(method, url, async, username, password)

Solution 2 - Javascript

There is a good article/tutorial written by Paul James. I used it some time ago and it worked for me.

HTTP Authentication with HTML Forms

> [...] XMLHTTPRequest, it can submit the > correct HTTP auth headers for us. > Rather than adjusting the URL the form > submits to, we can use XMLHTTPRequest > to do a request before the form > submits supplying the entered username > and password. > > This will set up the > browser with the HTTP auth credentials > so it'll also send them with our > actual form submission login request.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsystem PAUSEView Question on Stackoverflow
Solution 1 - JavascriptAlnitakView Answer on Stackoverflow
Solution 2 - JavascriptsplattneView Answer on Stackoverflow