【发布时间】:2020-06-24 15:31:27
【问题描述】:
当我在 expressjs 中发出 http post 请求时,我试图在服务器上设置 cookie,但是当我发出 http get 请求时,cookie 正在设置。我不知道可能是什么原因。以下是我的代码:
app.post('/tou', function(req, res) {
res.setHeader('Cache-Control', 'private');
res.cookie('__pal', 'locked', { httpOnly: false, path: '/', secure: false, expire: 6 * 60 * 60 * 1000 })
res.send('done')
})
【问题讨论】:
-
您确定它是在您发出 GET 请求时设置,还是在您发出 POST 请求时被设置然后从浏览器发回 到以后的 GET 请求中的服务器?
-
当我发出 GET 请求时正在设置
-
如果是,那代码不是原因。
-
我使用了与您完全相同的代码,它对 POST 请求有效!这就是我从客户端发出请求的方式;
var xhr = new XMLHttpRequest(); xhr.open("POST", '/tou', true); xhr.onreadystatechange = function() { // Call a function when the state changes. if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { console.log('succssss') } } xhr.send("foo");得到回复:Set-Cookie: __pal=locked; Path=/ -
请问你是如何在 expressjs 中设置来自服务器的 cookie
标签: javascript node.js express cookies