如何使用 NodeJS 在浏览器中弹出警告窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44915831/
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
how to use NodeJS pop up a alert window in browser
提问by phaneven
I'm totally new to Javascript and I'm wondering how to use node js to pop up a alert window in browser, after sever(Nodejs) received post message from front end? Do I need to use Ajax?
我对 Javascript 完全陌生,我想知道如何使用 node js 在浏览器中弹出警报窗口,在 sever(Nodejs) 收到来自前端的帖子消息后?我需要使用 Ajax 吗?
回答by Viran Malaka
"after sever(Nodejs) received post message from front end?" show a pop up in the browser. This can not be done. I assume you want to show a popup in if the post request is success. Because you mention about Ajax, This is how it is done.
“在服务器(Nodejs)收到来自前端的帖子消息之后?” 在浏览器中显示一个弹出窗口。这是做不到的。我假设您想在发布请求成功时显示一个弹出窗口。因为你提到了 Ajax,这就是它的做法。
in your post router definition in the server do it as follows
在服务器中的后路由器定义中执行如下操作
router.post('/path', function(req, res){
//do something
res.jsonp({success : true})
});
something like this. finally you want to send something form the server to the client. after in the client side javascript file send the post request as follows.
像这样的东西。最后你想从服务器向客户端发送一些东西。在客户端 javascript 文件中发送 post 请求后,如下所示。
$.ajax({
url:"/url/is/here",
method: "POST",
data : {
data : "what you want to send",
put : "them here"
},
cache : false,
success : function (data) {
// data is the object that you send form the server by
// res.jsonp();
// here data = {success : true}
// validate it
if(data['success']){
alert("message you want to show");
}
},
error : function () {
// some error handling part
alert("Oops! Something went wrong.");
}
});
回答by Aneek
There is a npmmodule for popups known as popups. You have to install it using the command npm install popups. Then use it in the following way:
有一个npm名为popups. 您必须使用命令安装它npm install popups。然后按以下方式使用它:
var popup = require('popups');
popup.alert({
content: 'Hello!'
});
You can find more information here
你可以在这里找到更多信息

