我正在开发一个react
应用程序,该应用程序从第三方网站接收了httponly
cookie。
我可以在Chrome开发人员console
中看到Cookie。
我正在尝试将此Cookie发送到内置于expressjs
的我的应用程序的后端,以便我可以读取Cookie。我正在使用fetch
向应用发送GET
请求,同时包含以下prop
:
Credentials: 'include'
在express
服务器中,将我的前端放入CORS
,
将凭据设置为true。
问题:
在我的express
服务器的请求标头中,看不到httponly
cookie。
有人可以指导我如何发送httponly
并将其放入express
服务器中吗?
答案 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);
答案 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