会话cookie不与XHR请求一起发送

时间:2020-01-11 08:11:23

标签: reactjs express cors express-session

(来自domain react-app.com域的)我的React应用程序已加载到beautiful-site.com网站中,并且从react应用程序发送的请求未发送与其关联的会话Cookie。

我已在服务器中以及在以withCredentials: true发送的react app XHR请求中添加了以下标头,以解决我的问题

Access-Control-Allow-Origin: https://beautiful-site.com
Access-Control-Allow-Credentials: true

问题是我可以在Access-Control-Allow-Origin中指定一个特定的站点,因为react应用程序将被添加到许多站点而不是一个站点。

有什么方法可以让React应用发送会话cookie吗?

1 个答案:

答案 0 :(得分:2)

答案是否定的,但是您有解决方法:设置withCredentials时,您将没有Access-Control-Allow-Origin: *

必须在服务器端完成此修复:您的API服务必须在Access-Control-Allow-Origin中返回请求的域,通常从Origin标头中获取。

请参阅No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API如何解决“ Access-Control-Allow-Origin标头一定不能成为通配符”问题小节),以获取技术详细信息。