4

I want to see if it is possible to log into a third site that uses HTTP authentication. Ideally, the browser will store the credentials. Unfortunately this fails every time. Any help would be much appreciated, I'm using the base64 Jquery plugin, which I've tested to work.

So, two questions:

  1. How can I view the HTTP status code?
  2. Will this ultimately work, in principle?

<script>
$("#login_button").click(function() {   
        var username = 'myFunUsername';
        var password = 'myFunPassword';

        $.ajax({
                url: 'http://remote-site-with-http-auth.com/member_site',
                beforeSend: function(xhr) {
                        xhr.setRequestHeader("Authorization", "Basic " + $.base64.encode(username + ":" + password));
                },
                success: function(data) { $("#label").text("Logged in, yay!"); }
        }).fail(function(){ $("#label").text("It didn't work"); });

});

Thanks!!

Bill
  • 2,319
  • 9
  • 29
  • 36
  • possible duplicate of [jquery cross domain authentication](http://stackoverflow.com/questions/10769791/jquery-cross-domain-authentication) – jamesmortensen May 27 '12 at 19:29
  • Hi B. VB, check out that link. The op was trying to do the same exact thing and was missing some headers in the response. Good luck! – jamesmortensen May 27 '12 at 19:30
  • [Accessing web Service from jQuery - cross domain](http://stackoverflow.com/questions/2697557/accessing-web-service-from-jquery-cross-domain) may be useful – Kevin Ji May 27 '12 at 19:35
  • I took a look at it but it doesn't really seem to solve the problem. In particular, using wireshark I don't even see the authorization header in the HTTP headers! – Bill May 28 '12 at 00:31
  • Why do you need the [status code](http://stackoverflow.com/questions/5344145/how-to-get-response-status-code-from-jquery-ajax)? – lsl May 31 '12 at 03:10

1 Answers1

2
var user= 'myFunUsername';
var pwd= 'myFunPassword';
        $.ajax({
                url: 'http://remote-site-with-http-auth.com/member_site',
                crossDomain:true,
                username :user,// Keep the variable name different from parameter name to avoid confusion
                password:pwd,
                xhrFields: { /* YOUR XHR PARAMETERS HERE */}

                beforeSend: function(xhr) {
                       // Set your headers
                },
                success: function(data, textStatus, xhr) {
                    alert(xhr.status);// The status code
                    //Code for success
                }
        }).fail(function(){ 
               //Fail code here
               });

For more details on parameters refer to http://api.jquery.com/jQuery.ajax/

Imdad
  • 5,942
  • 4
  • 33
  • 53