[英]Send cookie with get request
I 'm authenticating with site https://a.com .我正在使用站点https://a.com进行身份验证。 Once I authenticate, I access http://b.com .进行身份验证后,我将访问http://b.com 。 a.com sets a cookie for the session that I need to send with when I make a get request from b.com to a.com. A.com为Z21D6F40CFB511982E4424E250A9557Z设置了一个cookie,我需要从B.Z4D2236D9A2D102C5102C55FEER a时发送我需要发送的cookie。
Can you please tell me, how I can get the cookie and pass it on with my request.你能告诉我,我如何获取 cookie 并根据我的请求传递它。
I have tried sending the requests with "withCredentials": true, Also tried using the axios-cookiejar-support with tough-cookie.我尝试使用“withCredentials”发送请求:true,还尝试使用 axios-cookiejar-support 和 strong-cookie。 But doesn't seem to work.但似乎不起作用。
.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.
Why don't use a javascript function instead that helps use fetch the Cookie based on it's name like below为什么不使用 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"))
Have a look to this question .看看这个问题。
The problem you are trying to solve is to read/send cookies from/to different domains.您要解决的问题是从/向不同域读取/发送 cookies 。
One solution, as stated in the previously linked question would be to set some headers on http://b.com
如先前链接的问题所述,一种解决方案是在http://b.com
上设置一些标题
Access-Control-Allow-Origin: http://b.com
Access-Control-Allow-Credentials: true
You do that on the server that has the domain http://b.com
您在具有域http://b.com
的服务器上执行此操作
Something along those lines should solve the issue for you.这些方面的东西应该可以为您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.