未将Cookie添加到请求标头中

问题描述 投票:0回答:1

[就像再次询问CORS Cookie的问题一样,我已经花了很多时间,但无法解决。这是情况。

我在nodejs(http://localhost:5000)中有一个后端api,还有一个React Frontend应用程序[http://localhost:3000)。在后端,Cors设置是这样的。

private initializeCors(){
    this.app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "http://localhost:3000");
      res.header("Access-Control-Allow-Headers", "Content-Type, Accept");
      res.header("Access-Control-Allow-Credentials", "true");
      next();
    });
  }

我使用用户名/密码登录时在[提取Api中设置了{ credentials: "include" }。已设置Set-Cookie作为响应,我可以看到它已保存在浏览器中。Cookie的格式为“ Authorize = TOKEN; HttpOnly; Max-Age = 3600;”。Cookie in browser

然后,当路由到另一个URL时,它无法从401后端从后端检索数据。这是序列调用的代码。

const credentialsInclude : "include" | "omit" | "same-origin" | undefined = "include";
function getAllPayments() {
    const requestOptions = {
        method: 'GET',
        credentials: credentialsInclude
    };
    return fetch(apiUrls.GET_ALL_PAYMENTS, requestOptions).then(handleResponse);    
}

我可以看到Cookie没有添加到标题中。No cookie in header

我遵循了here的最佳答案,但无法使其正常工作。

有什么建议吗?谢谢。

cookies cross-domain
1个回答
0
投票

我刚刚想通了。此问题不是由CORS设置引起的。它是由Cookie本身引起的。就我而言,我需要在响应头中将Path=/;添加到Set-Cookie中。这样,可以将成功登录后来自响应的cookie添加到排序的请求中。

© www.soinside.com 2019 - 2024. All rights reserved.