javascript – JQuery中的跨站点HTTP身份验证

我想看看是否可以登录使用HTTP身份验证的第三个站点.理想情况下,浏览器将存储凭据.不幸的是,每次都失败了.任何帮助将不胜感激,我正在使用base64
Jquery插件,我已经测试过它.

那么,有两个问题:

>如何查看HTTP状态代码?
>原则上,这最终会起作用吗?

<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"); });

});

谢谢!!

最佳答案

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
               });

有关参数的更多详细信息,请参阅http://api.jquery.com/jQuery.ajax/

点赞