在JQuery的跨网站HTTP认证 [英] Cross site HTTP authentication in JQuery

查看:111
本文介绍了在JQuery的跨网站HTTP认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

于是,两个问题:

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

 <脚本>
$(#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:

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

解决方案

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/

这篇关于在JQuery的跨网站HTTP认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆