如何将主网站的安全会话 cookie 传递给 Web worker 并在那里使用它来调用 API



我们使用安全会话cookie,后端通过登录API设置cookie。Http请求是跨域的。我想使用网络工作者做一些计算和调用某些api。尽管使用了credentials: 'include', cookie在web worker API调用中还是会丢失。我能做什么,如果我不想传递回API调用主线程?

实际上,web workerHAS主线程cookie,包括安全的。要使用凭据,您应该在获取请求参数中提到credentials: 'include',如下所示:

fetch('https://example.com/profile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
// this line is important:
credentials: 'include', // include, same-origin or omit ... as your conditions

})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})

最新更新