[英]Send cookie with get request
我正在使用站点https://a.com进行身份验证。 进行身份验证后,我将访问http://b.com 。 A.com为Z21D6F40CFB511982E4424E250A9557Z设置了一个cookie,我需要从B.Z4D2236D9A2D102C5102C55FEER a时发送我需要发送的cookie。
你能告诉我,我如何获取 cookie 并根据我的请求传递它。
我尝试使用“withCredentials”发送请求:true,还尝试使用 axios-cookiejar-support 和 strong-cookie。 但似乎不起作用。
.get(a.com/path-requiring-cookie, {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'access-control-allow-credentials': true,
},
})
.then(res => {
console.log(res.data);
});```
This is the code that I have to send the request.
But this doesn't seem to send the cookie with the request. Can someone please clarify.
为什么不使用 javascript function 来帮助使用基于它的名称获取 Cookie,如下所示
function GetCookie(cName) { const name = `${cName}=`; const decodedCookie = decodeURIComponent(document.cookie); const ca = decodedCookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return ''; }; console.log(GetCookie("SID"))
看看这个问题。
您要解决的问题是从/向不同域读取/发送 cookies 。
如先前链接的问题所述,一种解决方案是在http://b.com
上设置一些标题
Access-Control-Allow-Origin: http://b.com
Access-Control-Allow-Credentials: true
您在具有域http://b.com
的服务器上执行此操作
这些方面的东西应该可以为您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.