React axios-Laravel后端的Cookie未被浏览器保存

时间:2018-12-04 17:26:18

标签: reactjs laravel cors axios setcookie

对于这个问题,我知道在Stackoverflow上有很多问题(和答案)。但不幸的是,没有答案能帮助我解决问题。

我有一个带有API和React前端的Laravel后端。现在,我要发出一个API请求。响应标头包含Set-cookie属性,但浏览器不会保存cookie!

我在laravel后端和axios前端调用中设置了Allow-Credentials

代码如下:

Laravel后端

Cors文件:

'supportsCredentials' => true,
    'allowedOrigins' => ['http://localhost:3000','http://dev.hefe:3000'],
    'allowedMethods' => ['GET','POST','PUT','DELETE'],

用于API请求的控制器

// Some code before return

        return response($output)
            ->header('Content-Type', 'application/json')
            ->cookie('refreshToken', $responseJSON['refresh_token'], 14400, null, null, true, true);
}

ReactJS前端

axios.post(`http://dev.hefe/api/login`,
           logindata,
           {headers: {"Content-Type": "application/json"},withCredentials:true})
     .then((res) => {//Some code})
     .catch((error) => { // Some error handling });

在这里,我还尝试了文件axios.defaults.withCredentials = true;的顶部-其他答案中建议使用该

最后,这是请求在我的浏览器中的输出:

Request Response

0 个答案:

没有答案