Reputation: 13972
Part of our website is protected with .htaccess style password protection. When you try to access this area of the website the web browser pops up a dialog asking for your username and password.
I need to access this programatically (eg with an ajax call). How does the server tell me it needs a password and how do I supply it?
Upvotes: 7
Views: 9910
Reputation: 151
jQuery supports HTTP authentication with their ajax() method. Something like this should work:
jQuery.ajax({
type: "GET",
url: "foo.php",
username: "foo",
password: "bar"
});
Documentation on all jQuery.ajax() options can be found here: http://docs.jquery.com/Ajax/jQuery.ajax#options
Upvotes: 4
Reputation: 10896
this is of course not ajax, but with wget client you can use --http-user and --http-password flags
Upvotes: 2
Reputation: 491
In general, http://user:[email protected]
But there are some obvious security shortcomings with that.
A more complete solution may be to set a Session variable after a user is authenticated.
Make the AJAX post to a script that checks authentication. If authenticated, use CURL to fetch the results using a pre-defined authorized account.
This allows you to re-use basic apache auth but prevents any passwords from being written in the DOM.
Upvotes: 3
Reputation: 24642
Basic HTTP authentication:
http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html
Upvotes: 11