会话存储返回 null
我有这个 post 方法,其中有变量 username,变量的内容我需要在另一个 js 文件中使用它。在这个方法中,我使用 sessionStorage.setItem()。
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
sessionStorage.setItem('user', JSON.stringify(username));
if (username && password) {
connection.query('SELECT * FROM usuarios WHERE nombre_usuario = ? AND password_usuario = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect('/home');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
然后,正如我所说,我需要另一个 js 文件中的用户名变量。为此,我使用 sessionStorage.getItem(),但始终返回 null。
case "based":
// INSERT INTO BD states, Q and type of automaton, M
console.log("the username is " + sessionStorage.getItem('user'));
var state = this.chart.obtainStates();
console.log ("the state is " + state);
var state2 = state.split("x",1);
console.log ("the state2 is " + state2);
}
}
我应该怎么办?
I have this post method where I have the variable username,variable whose content I need to use it in another js file.In this method I use sessionStorage.setItem().
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
sessionStorage.setItem('user', JSON.stringify(username));
if (username && password) {
connection.query('SELECT * FROM usuarios WHERE nombre_usuario = ? AND password_usuario = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect('/home');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
Then, as I have said I need the username variable in another js file. For that I use sessionStorage.getItem(), but always returns null.
case "based":
// INSERT INTO BD states, Q and type of automaton, M
console.log("the username is " + sessionStorage.getItem('user'));
var state = this.chart.obtainStates();
console.log ("the state is " + state);
var state2 = state.split("x",1);
console.log ("the state2 is " + state2);
}
}
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论