views:

4483

answers:

2

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

(Yes, I could read the spec and implement it myself, but it's for a side project and I'd rather not spend much time on it.)

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.

+4  A: 

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.

splattne
More than what I was looking for, but looks like a very nice way to replace the browser's default login dialog.
system PAUSE
+5  A: 

Take a look at the W3C specifications for the XMLHttpRequest Object. The five-parameter version of the 'open' method allow you to specify the username and password.

Alnitak
Exactly what I needed, and I should have RTFM! Thanks!
system PAUSE