reactjs - Set-Cookie 未在我的浏览器中设置 cookie (React + Sanctum)
问题描述
我正在使用 Laravel sanctum 在 cors.php 中使用 ReactJS 进行身份验证。我已经设置了这些值
'paths' => ['api/*', 'api/csrf-cookie','/login'],
'supports_credentials' => true,
圣所.php
'prefix'=>'api'
在 React 中,我将 axios 与凭证一起使用
axios.get('http://www.react.test/api/csrf-cookie').then(response => {
console.log(response);
}, { withCredentials: true });
使用 Postman 时,我得到了 Set-Cookie Value ,但是当我尝试使用浏览器时,它没有在我的浏览器中的 Application->cookies 选项卡下设置
- Laravel 域名
http://www.react.test/
- 反应域
http://localhost:3000/
解决方案
您使用的axios
包错误,withCredentials
应该作为 a config options
toaxios
而不是 a then
,
将您的代码更改为此
axios.get('http://www.react.test/api/csrf-cookie',{ withCredentials: true }) // FIX
.then(response => {
console.log(response);
}).catch((err)=>{
console.log(err);
});
这现在可以正常工作
更多请参考axios
推荐阅读
- python - 检查从客户端发送到服务器应用程序的消息之间的时间
- postgresql - Asp net - 使用 OnDelete() 从数据库中删除记录
- javascript - 使用 querySelector 获取导航以显示和隐藏并显示无/块?
- android - Unity - Gradle 构建错误:无法解析配置“:releaseCompileClasspath”的所有文件
- javascript - 如何从模态更新行的每一列的值?
- node.js - app.intent 中的 DialogFlow if 语句 conv.ask
- javascript - 如何获取字符串中特定字符前面或后面的文本
- css - 在 Office UI Fabric 中设置“PivotItem”的宽度
- c# - 在 .Net Core 中以编程方式在 Azure 上创建子域
- python - Keras 模型检测手绘不正确