无法使用PUG模板从引导程序模式发出POST请求

时间:2019-04-21 12:51:07

标签: node.js express pug

在Index.js中:

const express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const app = express();
var fs = require("fs");
app.set('view engine', 'pug');
app.set('views','./views');
app.use(bodyParser.json()); 

app.use(bodyParser.urlencoded({ extended: false }))
var contents = fs.readFileSync("issue.json");
var jsonContent = JSON.parse(contents);

app.get('/', function(req, res){
res.render('issueTracker',{result:jsonContent.issueList, 
product:'Products'});
});

app.post('/add', function(req, res){
  console.log("req.body");
  res.send("ddsfdf");
})

app.listen(3000);

PUG文件(views / issueTracker.pug)包含了所有CDN所需的

link(rel="stylesheet" href="/bootstrap/bootstrap.css")
link(rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
script(type='text/javascript', src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js")
script(type='text/javascript', src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js")
script(type='text/javascript', src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js")


button.btn.btn-primary.btn-sm(type='button', data-toggle="modal" 
data-target="#myModal" style='margin-top: 2%') Add Issue

#myModal.modal
    form(action = "/add", method = "POST")
        .modal-dialog
            .modal-content
                .modal-header
                    h4.modal-title ADD ISSUE
                        button.close(type='button' data-dismiss="modal") ×

                 .modal-body
                     .form-group
                         label(for='Description') Description
                                textarea#Description.form-control(rows='2' name="Description")
                         label(for='Severity') Severity
                         select#Severity.custom-select.mb-3(name="Severity")
                             option(value='Minor') Minor
                             option(value='Major') Major
                             option(value='Critical') Critical
                          label(for='Status') Status
                          select#Status.custom-select.mb-3(name="Status")
                              option(value='Open') Open
                              option(value='In Progress') In Progress
                              option(value='Closed') Closed
                           label(for='Created') Created Date
                           input#Created.form-control(type="date" name="Created")
                           label(for='Resolved') Resolved Date
                           input#Resolved.form-control(type="date" name="Resolved")
                           button.btn.btn-info(type='submit' value="Submit" data-dismiss="modal")
                           button.btn.btn-danger(type='button' data-dismiss='modal') Close

我得到了“ get” api响应,但是“ post” api根本没有被点击。当我在引导程序模态中按Submit按钮时,我需要提交表单数据,但是问题是post api没有被点击。请帮助我,谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下。 (已删除data-dismiss="modal"

button.btn.btn-info(type='submit' value="Submit")