使用 Node.js 在浏览器中显示警报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27812639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display alert message in browser using Node.js
提问by Jayanth
I need to display an alert message in browser using Node.js code. Here my code
我需要使用 Node.js 代码在浏览器中显示警报消息。这是我的代码
app.post('/logon', function (req, res)
{
var user=req.body.userName;
var password=req.body.password;
if((user=='admin')&&(password=='admin'))
{
//redirect to home page
}
else
{
//here I need to display a alert message in browser saying that invalid user name or password is wrong
}
..........
回答by surajck
If you're using ajaxto send the user name and password to the node server, then you can do a
如果您使用的ajax是将用户名和密码发送到节点服务器,那么您可以执行以下操作
app.post('/logon', function (req, res)
{
var user=req.body.userName;
var password=req.body.password;
if((user=='admin')&&(password=='admin'))
//redirect to home page
else
res.send(500,'showAlert')
on the server and in the errorfunction of the ajax on the client side, do
在服务器端和error客户端ajax的功能中,做
error: function(error){
if(error.responseText == 'showAlert')
alert("Please enter correct user name and password.")
Like Felix Kling pointed out, this needs to be executed on the client side.
就像 Felix Kling 指出的那样,这需要在客户端执行。

