[英]Set-Cookie header not accepted by javascript fetch
I've been trying for a couple of days now to set a cookie from my web server written in Go, and everything looks right but it just won't work.几天来,我一直在尝试从我的 web 服务器中设置一个 cookie,该服务器用 Go 编写,一切看起来都正确,但它就是行不通。
My Set-Cookie header:我的 Set-Cookie header:
Set-Cookie: session_token=token; Path=/; Expires=Date; Max-Age=120000; HttpOnly; SameSite=Strict
I've tried editing all the values and removing and adding other fields but nothing helped.我尝试编辑所有值并删除和添加其他字段,但没有任何帮助。 If I set my request to get and navigate directly to the link with browser cookie appears, and in Postman it is there, but when I try using fetch to get the http request it doesn't work.
如果我将请求设置为获取并直接导航到带有浏览器 cookie 的链接,并且在 Postman 中它就在那里,但是当我尝试使用 fetch 来获取 http 请求时它不起作用。 I know about
credentials: "same-origin"
and credentials: "include"
but they don't work.我知道
credentials: "same-origin"
和credentials: "include"
,但它们不起作用。 Here is my js fetch code:这是我的 js 获取代码:
const log_in = (e) => {
// data entry validation and stuff
const url = "/signin";
fetch(ip + url, {
credentials: "same-origin",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: document.getElementById("username").value,
password: document.getElementById("password").value,
}),
}).then(
() => console.log("recieved")
);
};
This is not at all what I wanted but this is how I solved my problem if somebody is in the same boat as me... Using axios.这根本不是我想要的,但如果有人和我在同一条船上,这就是我解决问题的方法......使用 axios。 I don't use node, yarn or whatever and using cdn gave me cors errors so I just copied the code from cdn file locally and that worked.
我不使用节点、纱线或其他任何东西,并且使用 cdn 给了我 cors 错误,所以我只是从本地 cdn 文件中复制了代码,并且工作正常。 You can se the code here:
您可以在此处查看代码:
axios({
method: 'post',
url: ip + url,
data: {
firstName: document.getElementById("username").value,
lastName: document.getElementById("password").value
},
withCredentials: true
})
I won't mark this as an answer since I wanted to do this with fetch and no 3rd party libraries but if this is someone's last resort this works...我不会将此标记为答案,因为我想使用 fetch 并且没有 3rd 方库来执行此操作,但如果这是某人的最后手段,这可行...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.