Use basic authentication with jQuery and Ajax

I am trying to create a basic authentication through the browser, but I can’t really get there.

If this script won’t be here the browser authentication will take over, but I want to tell the browser that the user is about to make the authentication.

The address should be something like:

http://username:password@server.in.local/

I have a form:

<form name="cookieform" id="login" method="post">
      <input type="text" name="username" id="username" class="text"/>
      <input type="password" name="password" id="password" class="text"/>
      <input type="submit" name="sub" value="Submit" class="page"/>
</form>

And a script:

var username = $("input#username").val();
var password = $("input#password").val();

function make_base_auth(user, password) {
  var tok = user + ':' + password;
  var hash = Base64.encode(tok);
  return "Basic " + hash;
}
$.ajax
  ({
    type: "GET",
    url: "index1.php",
    dataType: 'json',
    async: false,
    data: '{"username": "' + username + '", "password" : "' + password + '"}',
    success: function (){
    alert('Thanks for your comment!');
    }
});

71 thoughts on “Use basic authentication with jQuery and Ajax”

  1. I believe everything said made a ton of sense.
    However, what about this? what if you were to write a awesome headline?
    I mean, I don’t wish to tell you how to run your website, however what if you added a title that makes people want more?

    I mean Use basic authentication with jQuery and Ajax
    – JavaScript Help is a little boring. You could peek at Yahoo’s
    home page and watch how they write article headlines to get people to click.

    You might add a related video or a related pic or two to grab readers excited about everything’ve written. Just my
    opinion, it could make your posts a little livelier.

    Reply
  2. Oh my goodness! Amazing article dude! Many thanks, However I am having troubles with
    your RSS. I don’t understand why I am unable to subscribe to it.
    Is there anybody having similar RSS issues? Anyone
    that knows the answer will you kindly respond?
    Thanks!!

    Reply
  3. Howdy I am so glad I found your blog page, I really found
    you by accident, while I was browsing on Digg for something else, Regardless I am here now and
    would just like to say many thanks for a remarkable post and a all round thrilling blog
    (I also love the theme/design), I don’t have time to browse it
    all at the moment but I have saved it and also included your RSS
    feeds, so when I have time I will be back to read much more, Please do keep up the excellent work.

    Reply
  4. Thank you for another informative web site. Where else may I
    am getting that type of information written in such an ideal manner?
    I’ve a project that I am just now operating on, and I’ve been on the look out for such info.

    Reply
  5. An impressive share! I have just forwarded this onto a co-worker who has been conducting a little research on this.
    And he in fact bought me lunch simply because I found it for him…

    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to talk about this
    issue here on your web page.

    Reply
  6. Generally I do not learn article on blogs, but I would
    like to say that this write-up very compelled me to take a look at and do so!
    Your writing taste has been surprised me. Thank you, quite nice article.

    Reply
  7. Thanks , I have recently been searching for
    info approximately this subject for a while and yours is the greatest I have discovered till now.
    But, what concerning the conclusion? Are you positive in regards
    to the source?

    Reply
  8. Hi there very cool website!! Guy .. Beautiful .. Wonderful ..
    I will bookmark your website and take the feeds also?
    I am glad to seek out numerous useful info here within the post, we’d like work
    out extra strategies on this regard, thanks for sharing.
    . . . . .

    Reply
  9. Thank you for the auspicious writeup. It actually was
    once a leisure account it. Glance complicated to more added agreeable from you!

    However, how could we be in contact?

    Reply
  10. Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a
    lot of spam remarks? If so how do you prevent it,
    any plugin or anything you can suggest? I get so much lately it’s driving me insane so any help is
    very much appreciated.

    Reply
  11. Early to bed and early to rise makes a man healthy, wealthy, and wise.
    Speaking of being safe, that’s another aphorism example that you’ve probably heard before.
    It’s become one of the most viral memes on the internet.
    Napoleon Bonaparte could relate.
    Yup, he was reminding Philadelphians that preventing fires is be