如何使用NodeJS在浏览器中弹出一个警告窗口

问题描述 投票:0回答:3

我是Javascript的新手,我想知道如何使用节点js在浏览器中弹出警报窗口,在服务器(Nodejs)收到来自前端的帖子后?我需要使用Ajax吗?

node.js
3个回答
3
投票

“在服务器(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.");
    }
});

3
投票

有一个npm模块用于称为popups的弹出窗口。您必须使用命令npm install popups安装它。然后以下列方式使用它:

var popup = require('popups');

popup.alert({
    content: 'Hello!'
});

你可以找到更多信息here


-7
投票

你可以干脆做

alert("Hellow World")

这是https://www.w3schools.com/jsref/met_win_alert.asp的教程

© www.soinside.com 2019 - 2024. All rights reserved.