0

I am trying to bring up the HTTP authentication box for handling my login. However, I want to communicate to my database through Ajax. But I dont know how to connect the HTTP authentication box with AJAX.

Any Idea, how to do this? Please also include example

3
  • 2
    Is this some kind of a joke or you are serious about this question? Commented Aug 1, 2010 at 9:24
  • no no, I want to communicate HTTP login box with jquery Commented Aug 1, 2010 at 9:27
  • 1
    Oh right, coz I thought you were joking, not that this changes anything but you could always hope for answers. With this level of details in your question I wouldn't :-) Commented Aug 1, 2010 at 9:28

2 Answers 2

1

(To my surprise) jQuery has built-in support for supplying HTTP auth credentials:

$.ajax({
    url: "http://example.org/",
    username: "NAMENAMENAMENAMENAME",
    password: "PWPWPWPWPWPWPWPWPWPW",
    ...
})

Or you can override the XMLHttpRequest object like in this example: http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html

Sign up to request clarification or add additional context in comments.

1 Comment

@asdf2: jQuery 1.4.2 works fine for me with username: and password:
1

Basic HTML:

<form id="login-form" action="/login.php" method="post">
    <label for="username">Username: </label> 
    <input type="text" name="username" id="username"/><br />

    <label for="Password">Password: </label> 
    <input type="text" name="password" id="password"/><br />

    <input type="submit" value="Login"/>
</form>​

Basic jQuery:

$(document).ready(function() {
    $('#login-form').bind('submit', function (e) {
        var self = $(this);

        // See ajax: http://api.jquery.com/jQuery.post
        // See serialize: http://api.jquery.com/serialize()
        jQuery.post(self.attr('action'), self.serialize(), function () {
            if (response.success) {
                // wooo, logged in
            } else {
                alert("Invalid username or password. Please try again");
            }
        }, 'json');

        e.preventDefault(); //prevent the form being posted 
    });
});​

Basic PHP:

<?php
if ($_POST['username'] == 'Admin' && $_POST['password'] == 'letmein') {
    echo json_encode(array('success' => true));
} else {
    echo json_encode(array('success' => false));
};
?>

A little bit of data validation wouldn't go amiss.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.