这是我的代码:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials', true);
next();
});
app.post('/cookie',function(req, res){
res.cookie('test', 'cookie_value', {httpOnly: false});
return res.send('cookie has been set!');
});
那是在服务器端。 作为客户端,我正在发送POST请求:
fetch('https://MyServer.com/cookie', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(userData)
});
我想将Cookie作为客户端。问题是我可以得到“已设置cookie!”作为答复,但没有cookie,也没有“ Set-Cookie”标头或类似的内容。在“应用程序”选项卡中查看F12 devtools时,我发现没有cookie,“网络”选项卡中的响应也如下所示:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Origin: http://localhost:3000
Connection: keep-alive
Content-Length: 20
Content-Type: text/html; charset=utf-8
Date: Thu, 14 Mar 2019 12:21:35 GMT
ETag: W/"14-F3kI2UkV9f/5WImUwr2a1XDtzl4"
X-Powered-By: Express
怎么了?