在JQuery的跨网站HTTP认证网站、JQuery、HTTP

2023-09-11 22:30:19 作者:猪东西

我想看看是否可以登录到一个使用HTTP认证的第三位点。理想情况下,浏览器会保存凭证。不幸的是失败每次。任何帮助将是非常美联社preciated,我使用的base64 jQuery插件,这是我测试过的工作。

于是,两个问题:

如何查看HTTP状态code? 请问这个最终的工作原则?

 <脚本>
$(#login_button)。点击(函数(){
        VAR用户名='myFunUsername';
        VAR密码='myFunPassword';

        $阿贾克斯({
                网址:http://remote-site-with-http-auth.com/member_site,
                beforeSend:功能(XHR){
                        xhr.setRequestHeader(授权,基本+ $ .base64.en code(用户名+:+密码));
                },
                成功:功能(数据){$(#标签)文本(!登录,耶); }
        })失败(函数(){$(#标签)文本(它没有工作);});

});
 

谢谢!

解决方案

  VAR用户='myFunUsername';
VAR PWD ='myFunPassword';
        $阿贾克斯({
                网址:http://remote-site-with-http-auth.com/member_site,
                跨域:真正的,
                用户名:用​​户,//保留的变量名不同的参数名称,以避免混淆
                密码:PWD,
                xhrFields:{/ *您XHR此处的参数* /}

                beforeSend:功能(XHR){
                       //设置你的头
                },
                成功:功能(数据,textStatus,XHR){
                    警报(xhr.status); //状态code
                    // code成功
                }
        }),失败(函数(){
               //失败code在这里
               });
 

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

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:

How can I view the HTTP status code? 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!!

解决方案

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/

 
精彩推荐
图片推荐