我是Javascript的新手,我想知道如何使用节点js在浏览器中弹出警报窗口,在服务器(Nodejs)收到来自前端的帖子后?我需要使用Ajax吗?
“在服务器(Nodejs)收到来自前端的帖子后?”在浏览器中显示弹出窗口。这是不可能做到的。如果发布请求成功,我假设你想显示一个弹出窗口。因为你提到了Ajax,这就是它的完成方式。
在服务器的后置路由器定义中,执行如下操作
router.post('/path', function(req, res){
//do something
res.jsonp({success : true})
});
这样的事情。最后你想从服务器向客户端发送一些东西。在客户端javascript文件发送后请求如下。
$.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.");
}
});
有一个npm
模块用于称为popups
的弹出窗口。您必须使用命令npm install popups
安装它。然后以下列方式使用它:
var popup = require('popups');
popup.alert({
content: 'Hello!'
});
你可以找到更多信息here