Question

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!!

Était-ce utile?

La solution

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/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top