【问题标题】:How to insert cookie value into Authorization header before xmlHTTPRequest?如何在 xmlHTTPRequest 之前将 cookie 值插入到 Authorization 标头中?
【发布时间】:2019-11-05 15:41:47
【问题描述】:

预期结果图像;请查看以便更好地理解:

请在下面找到代码;当用户单击按钮时,我会获得除授权标头之外的所有 HTTP 标头,没有这些标头我无法获得响应,因此我使用 xhttp.setRequestHeader ('Authorization', 'Bearer') 对其进行硬编码,但我需要它的值;每个用户将有不同的授权值。 请在快照中找到所有标题。

我已经在代码中硬编码了 Authorization 标头。

<html>
<body>
<button type='button' onclick='cors()'>CORS</button>
<p id='demo'></p>
<script>
function cors() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var a = this.responseText;
document.getElementById("demo").innerHTML = a;
xhttp.open("POST", "http://xyz . com", true);
xhttp.withCredentials = true;
console.log(a);
xhttp. send("data="+a);
}
};
xhttp.open("GET", "https://api. xyz. com/v1/users/", true);
xhttp.withCredentials = true;
xhttp.setRequestHeader("Authorization", "Bearer ");
xhttp.send();

}
</script>
</body>
</html>

我只想将 cookie `[test_token] 值添加到授权标头中;添加 test_token 后,标头应类似于“授权:承载 test_token” 在显示响应之前,应将 cookie 添加到授权标头中。

【问题讨论】:

    标签: javascript ajax


    【解决方案1】:

    你不能。

    为了将 cookie 插入 Authorization 标头,您需要 read it with JavaScript

    由于 cookie 在传出请求上,并且这是一个跨域请求,因此 cookie 设置在不同的源上,因此 you can't read it

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2015-05-10
      • 2011-10-15
      • 2021-03-27
      相关资源
      最近更新 更多