Reactjs Flask会话无法在react前端应用程序上创建cookie(使用设置cookie响应头) 问题
我试图使用服务器端会话(保存在PSQL db上),但它们不会在请求之间持久化 描述 我正在本地运行我的应用程序,它由两部分组成Reactjs Flask会话无法在react前端应用程序上创建cookie(使用设置cookie响应头) 问题,reactjs,flask,wsgi,setcookie,flask-session,Reactjs,Flask,Wsgi,Setcookie,Flask Session,我试图使用服务器端会话(保存在PSQL db上),但它们不会在请求之间持久化 描述 我正在本地运行我的应用程序,它由两部分组成 在我的IP:2501上运行的后端 在我的IP:3000上运行的前端 现在根据我的理解,Flask将会话保存在PSQL的“session”表中(因为我们存储的是服务器端会话),并且来自该特定行的ID以响应头的形式发送到客户端,即“Set Cookie” 上面描述的每件事都在工作,但是当React前端(或浏览器)启动时接收此标头时,它不会从中创建cookie,因为会话id未
`response.headers.add('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept, x-auth")`
withCredentials=true
Access Control Allow Credentials=true
标题import axios from 'axios';
export const restApi = axios.create({
baseURL: urlBuilder.REST,
withCredentials: true
});
restApi.interceptors.request.use(
function(config) {
config.headers.withCredentials = true; # Sending request with credentials
return config;
},
function(err) {
return Promise.reject(err);
}
);
阿波罗上(前端图)
在Python烧瓶(后端)
如何与后端通信?是从浏览器还是通过前端?我一直在使用
axios
库向后端发出请求,并且我必须手动设置传出请求的会话头。我使用axios
从reactJS
前端发送请求。我还知道我必须手动设置传出请求的会话标头。我面临的问题是,我的后端正在发送set-Cookie响应标头,但前端/浏览器不允许保存Cookie。请在此处添加一些说明。我制作了一个基本应用程序,演示了此方法确实有效,您可以下载在这里刊登广告:
import {
ApolloClient,
ApolloLink
} from 'apollo-boost';
const authLink = new ApolloLink((operation, forward) => {
operation.setContext({
fetchOptions: {
credentials: 'include' . # Sending request with credentials
}
});
return forward(operation);
});
@app.after_request
def middleware_for_response(response):
# Allowing the credentials in the response.
response.headers.add('Access-Control-Allow-Credentials', 'true')
return response