发送授权头使用jQuery和Ajax [英] Sending authorization headers with jquery and ajax

查看:844
本文介绍了发送授权头使用jQuery和Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面看着的问题在这里计算器在什么即时尝试做没有运气。

I have looked at the following questions here on stackoverflow with no luck in what im trying to do.

<一个href=\"http://stackoverflow.com/questions/9820170/ajax-authorization-request-headers-fails-again-and-again\">Ajax授权请求头再次失败,再

jQuery的阿贾克斯未经授权401错误

<一个href=\"http://stackoverflow.com/questions/2054316/jquery-sending-credentials-with-cross-domain-posts\">jQuery:跨域的帖子发送凭据?

下面是我的code,我目前有:
    

Here is my code that I currently have:

    $(document).ready(function() {
        $.ajax({
            url: 'http://sample.domain.com/script.php?name1=value1&jsonp=?',
            type: 'GET',
            dataType: 'json',
            contentType: "application/json",
            beforeSend: function(xhr) {
                 xhr.setRequestHeader("Authentication", "Basic ZnJvbWFwcGx********uOnRoM24zcmQ1UmgzcjM=") //Some characters have been replaced for security but this is a true BASE64 of "username:password"
            },
            success: function(data){
                alert(data);
            }
        });
    });


</script>

子站点我已经是由htpasswd文件进行密码保护。该网站的登录使用为Base64使用EN code中的用户名/密码组合的作品就好了我。

The subdomain I have is password protected by an .htpasswd file. The login of the site works just fine for me using the username/password combo used in the base64 encode.

Im在一个比该URL上不同的域运行此脚本就是为什么我的JSONP =?在网址

Im running this script on a different domain than the one that the url is on which is why i have the jsonp=? in the url

响应IM浏览器从控制台得到的是:
     GET http://sample.domain.com/script.php?name1=value1&jsonp=jsonp1334177732136 401(需要授权)

The response im getting from the console in the browser is: GET http://sample.domain.com/script.php?name1=value1&jsonp=jsonp1334177732136 401 (Authorization Required)

推荐答案

JSONP使用脚本标记代理。它不支持自定义页眉。您在端点的控制权?如果是这样考虑启用CORS,或通过在GET字符串中的身份验证密钥。

JSONP uses a script tag proxy. It wouldn't support custom headers. Are you in control of the endpoint? If so look into enabling CORS, or pass the authentication key in the GET string.

在使用JSONP的JQuery您的网址Ajax的要求转化为:

When using jsonp JQuery converts your URL "ajax" request to:

<script src="[endpoint]"></script>

它然后写入随机函数

it then writes a random function

var json9409d0sf0d9s0df90 = function() {
     //some callback logic here.
}

和追加?回调= json9409d0sf0d9s0df90

and appends ?callback=json9409d0sf0d9s0df90

您的服务器,然后说:

echo $_GET['callback] . "(" . $json . ")";

并发送回如此

json9409d0sf0d9s0df90({some: data});

是由浏览器exexcuted和jQuery的神奇处理程序,使它像一个正常的回调ajax的响应处理。

is exexcuted by the browser and handled by jQuery's magical handlers that make it respond like a normal ajax callback.

据我所知,&LT; SCRIPT SRC =&GT;&LT; / SCRIPT&GT; 资产负载通过任何标准浏览器不会采取定制标题

AFAIK, <script src=""></script> asset loads wont take custom headers by any standard browser.

这篇关于发送授权头使用jQuery和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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