【发布时间】:2020-02-09 20:32:13
【问题描述】:
我尝试在我的 node.js 应用程序中使用发布请求,但由于某种原因我找不到它不起作用。也许你可以帮我解决这个问题
您可以在下面找到我的 report.js 和我的 report.ejs 文件:
report.js 文件:
var express = require('express');
var router = express.Router();
const { Pool, Client } = require("pg");
var app = express();
var bodyParser = require("body-parser");
/* GET home page. */
router.use(bodyParser.urlencoded({ extended : false }));
router.get('/', function(req, res, next) {
res.render('report', { title: 'Express' });
});
router.post('/top', function (req, res) {
console.log(req.body.todo + " is added to top of the list.");
res.redirect('/');
});
router.post('/bottom', function (req, res) {
console.log(req.body.todo + " is added to bottom of the list.");
res.json({ status: 'success' });
res.redirect('/');
});
module.exports = router;
report.ejs 文件:
<body>
<form>
<div class="form-group">
<label>To Do:</label>
<input type="text" class="form-control" name="todo">
</div>
<button type="submit" class="btn btn-primary btn-lg" formaction="/top" method="POST">Add to Top</button>
<button type="submit" class="btn btn-primary btn-lg" formaction="/bottom" method="POST">Add to Bottom</button>
</form>
<script src="/javascripts/reportjs.js"></script>
</body>
</html>
我收到两个按钮的错误消息:
GET /report 304 10.606 ms - -
GET /stylesheets/report.css 304 2.708 ms - -
GET /javascripts/reportjs.js 304 0.739 ms - -
GET /stylesheets/headder.css 304 1.229 ms - -
GET /bottom?todo=asdasd 404 2.253 ms - 145
Cannot GET /bottom
我不明白为什么当我使用 POST 方法时应用程序使用的是 GET 方法
【问题讨论】:
-
这应该与您的 node.js 代码无关,问题出在客户端。
-
ok :) 但是您知道如何检查客户端的问题吗?在这种情况下浏览器有问题吗?不应该。
标签: javascript node.js express .post