简体   繁体   English

如何从客户端向服务器发送cookie?

[英]how to send cookies from client to server?

I'm using MEAN Stack. 我正在使用MEAN Stack。 I made login/signup and profile page. 我做了登录/注册和个人资料页面。 I'm saving user id in cookies and now I want to send my id in server. 我正在将用户ID保存在cookie中,现在我想在服务器中发送我的ID。

I'm getting my id from cookies like that: 我从这样的cookie中获取我的ID:

userId = $cookies.get('userId')

How can i send this information to server ? 我如何将这些信息发送到服务器? I must find this information in DB with id like that: 我必须在ID为这样的数据库中找到此信息:

var userId = "receivedId";
User.UserModel.findOne({ _id: userId }, function(err, data) {
    var User = {_id: data._id, user: data.user, date: data.date};
res.send(User); //I know how to send from server to client.
});

Please, check your web server documentation. 请检查您的Web服务器文档。 If you're using express, you'll have to configure cookie-parser middleware first. 如果使用Express,则必须首先配置cookie解析器中间件。 After that you'll be able to access client cookies using req.cookies. 之后,您将能够使用req.cookies访问客户端cookie。 – Leonid Beschastny – Leonid Beschastny

answers author is: Leonid Beschastny 答案作者是:Leonid Beschastny

thank you. 谢谢。 it works! 有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM