app.post('/user/signin',function(req,res){
var user=req.body.user;
var {name,password}=user;
User.findOne({name:name},function(err,user){
if(err) console.log(err);
if(!user) return res.json({state:1});
user.comparePassword(password,function(err,isMatched){
if(err) console.log(err);
if(isMatched) {
res.json({state:3});
}
else res.json({state:2});
});
});
});
$('#signinModal .btn-success').on('click',function(e){
$.ajax({
type:'POST',
url:'/user/signin',
data:$('#signinModal form').serialize(),
success:function(data){
switch(data.state){
case 1:$('#errorName').css({opacity:1});break;
case 2:$('#errorPassword').css({opacity:1});break;
case 3:location.href='/';break;
}
}
});
});
You can assign user information to req.session.user through req.session.user = userInfo
When you jump to the homepage, you can get user information through req.session.user.
If your front-end page needs it, you can pass the value during rendering, such as
If you are using the express framework, you can use res.locals.user = req.session.user;
In this way, your page can be obtained directly by using the user variable, such as:
(Take the java backend as an example) After the front-end ajax submits the form (
var loader = new net.AjaxRequest(url, deal_data, onerror, "POST", params);
) [Note: net.AjaxRequest is a predefined function, where deal_data is the callback processing function after the ajax request is successful], the backend performs business After processing, use JSONArray to place the results to be returned, write httpservletresponse into it, and return it to the front end,Processed by the ajax callback function: get the return text, parse it into json data, and process it as needed.