无法从React JS发送httponly cookie到快递服务器

时间:2020-08-11 07:38:42

标签: node.js reactjs express

我正在开发一个react应用程序,该应用程序从第三方网站接收了httponly cookie。

我可以在Chrome开发人员console中看到Cookie。 我正在尝试将此Cookie发送到内置于expressjs的我的应用程序的后端,以便我可以读取Cookie。我正在使用fetch向应用发送GET请求,同时包含以下prop

Credentials: 'include' 

express服务器中,将我的前端放入CORS, 将凭据设置为true。

问题
在我的express服务器的请求标头中,看不到httponly cookie。

有人可以指导我如何发送httponly并将其放入express服务器中吗?

2 个答案:

答案 0 :(得分:1)

在客户端上,您还必须启用凭据。有axios模块用于使用凭据发出请求。用法示例:

import axios from 'axios'

const instance = axios.create({
  withCredentials: true,
  baseURL: API_SERVER
})

instance.get('todos')

通过其他方式,您可以为cookie提供XMLHttpRequest

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);

XMLHttpRequest

答案 1 :(得分:0)

第一件事是允许我们的Express应用程序能够接收来自主机的请求,在这种情况下,Fetch API会在该主机上从https:// localhost:8080进行调用

const express = require('express');
const cors = require('cors');

app.use(cors({
origin: 'http://localhost:8080',
credentials: true
}));

最后一件事是通过Fetch API从[https:// localhost:8080]到[http:// localhost:9090 / example]创建提取请求:

fetch('http://localhost:9090/example',{
method: ‘GET’,
credentials: 'include'
});

现在无论我们从另一个主机发出请求,我们都会收到Cookie