如何在Node.js中使用Sweetalert?

时间:2019-06-11 11:07:56

标签: node.js express ejs sweetalert2

我有一个带有html代码的nodejs代码,我想在nodejs中处理一个函数后在客户端显示一个甜蜜的警报?

var express = require('express');
var router = express.Router();
const Swal = require('sweetalert2');

router.post('/add', function(req, res, next) {

    Swal('Hello world!');

});

<!DOCTYPE html>
<html lang="pt_br">
<head>
</head>
<body>
    <h1 class="text-center title-1"> Cad </h1>
    <form action="/add" method="post">
        <input type="submit" value="Save"/>
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

您可以在这里使用EJS

SQL> -- Looking for number "10"
SQL>
SQL> declare
  2    l_str   varchar2(500);   -- SELECT statement to be used as dynamic SQL
  3    l_value number;          -- value you are looking for
  4    l_cnt   number;          -- number of rows that contain L_VALUE value
  5  begin
  6    -- I'm looking for a number 10 in current schema
  7    l_value := 10;
  8
  9    -- Loop through all tables; I'm restricting the list to only two tables.
 10    -- You'd probably want to remove the WHERE clause
 11    for cur_t in (select table_name
 12                  from user_tables
 13                  where table_name in ('EMP', 'DEPT')
 14                 ) loop
 15      -- Loop through all columns in every table from CUR_T
 16      for cur_c in (select column_name
 17                    from user_tab_columns
 18                    where table_name = cur_t.table_name
 19                      -- Adjust data type, according to L_VALUE value because if
 20                      -- you're looking for a string, you can't just compare it to
 21                      -- e.g. DATE datatype; you'll get the "invalid number" error
 22                      and data_type = 'NUMBER'
 23                   ) loop
 24        -- Compose a SELECT statement. It might differ, depending on value you're
 25        -- looking for. Strings have to be enclosed into single quotes (CHR(39))
 26        l_str := 'select count(*) from ' || cur_t.table_name ||
 27                 ' where '|| cur_c.column_name || ' = ' ||
 28                 l_value;
 29
 30        -- To make sure it is correctly written, display it first using
 31        -- dbms_output.put_line(l_str);
 32
 33        -- Run the SELECT statement
 34        execute immediate l_str into l_cnt;
 35
 36        if l_cnt > 0 then
 37           -- "10" was found in some table; display it
 38           dbms_output.put_line(cur_t.table_name ||'.'|| cur_c.column_name ||
 39                                ' contains ' || l_cnt || ' values I am looking for');
 40        end if;
 41      end loop;
 42    end loop;
 43  end;
 44  /
DEPT.DEPTNO contains 1 values I am looking for
EMP.DEPTNO contains 3 values I am looking for

PL/SQL procedure successfully completed.

SQL>

在HTML端

var express = require('express');
var router = express.Router();
router.post('/add', function(req, res, next) {
   res.status(201).render('new', { isAdded : true } );
});

答案 1 :(得分:1)

这是显示弹出小号的唯一方法

var express = require('express');
var router = express.Router();

router.post('/add', function(req, res, next) {

   res.json("Hello world!")
});


<!DOCTYPE html>
<html lang="pt_br">
<head>
</head>
<body>
    <h1 class="text-center title-1"> Cad </h1>
    <form id="form" action="#" method="post">
        <input type="submit" value="Save"/>
    </form>
</body>
</html>
<script>

//import JQuery from script
//import swal script

$("#form").on("submit", function(e){
e.preventDefault();

$.ajax({
  url: "/add",
  method: "post"
}).done(d=>{
  swal(e.responseJSON);
});
})
</script>

答案 2 :(得分:0)

为此,您可以使用查询参数。

所以,这就是你可以做的

在服务器上

var express = require('express');
var router = express.Router();
router.post('/login', (req, res)=>{
   res.redirect("/login?success=true&message=Logged In Successfully")
});

在客户端(EJS)上

    <script>
    var urlParams = new URLSearchParams(window.location.search);
    if(urlParams.has('success') && urlParams.get('success')){
    swal({
    title: "Failed",
    text: `${message}`, 
    icon: "error",
    button: "Okay",
    }).then(()=>{
        console.log(window.location.hostname)
    window.location.replace(window.location.origin + '/login');

    })
}

这只会弹出swal。而且,您可以切换成功的值以呈现错误和成功消息。