Express POST 请求正文输出未识别和 {}
Express POST request body outputting unidentified and {}
我很难检索到 post 请求表格 HTML 表格的正文。在我的代码中,我尝试以 JSON.stringify 的方式正常提取它。仍然没有骰子,只是输出:“”主体:app.js 中收到的“未定义”! ------- 正文:“[object Object]”在app.js!”中收到,控制台输出未定义和{}。任何帮助表示赞赏。下面是我的 JS 代码和 HTML 代码。
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(express.urlencoded({
extended: true
}))
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, './index.html'));
})
app.post('/submit-form', function(req, res) {
var x = req.body.key;
var y = req.body;
console.log(x);
console.log(y);
console.log(JSON.stringify(x));
console.log(JSON.stringify(y));
res.send(`Body: "${x}" recieved in app.js! ------- Body: "${y}" recieved in app.js!`);
})
app.listen(4001, function() {
console.log("Server is listening on port 4001...");
})
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample Site</title>
<style>
body { padding-top: 50px; }
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>res.sendFile() Works!</h1>
<form method="POST" action="/submit-form", body= "key: 'hellow wolrd'"}>
<input type="submit">
</form>
</div>
</div>
</body>
</html>```
POST 请求的主体由表单中指定表单控件(input
、select
等元素)的值确定。
<form>
元素没有 body
属性。
MDN has a forms + express tutorial 我推荐你阅读。
<form method="POST" action="/submit-form">
<input name=key value="hello, world">
<button>Submit</button>
</form>
我很难检索到 post 请求表格 HTML 表格的正文。在我的代码中,我尝试以 JSON.stringify 的方式正常提取它。仍然没有骰子,只是输出:“”主体:app.js 中收到的“未定义”! ------- 正文:“[object Object]”在app.js!”中收到,控制台输出未定义和{}。任何帮助表示赞赏。下面是我的 JS 代码和 HTML 代码。
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(express.urlencoded({
extended: true
}))
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, './index.html'));
})
app.post('/submit-form', function(req, res) {
var x = req.body.key;
var y = req.body;
console.log(x);
console.log(y);
console.log(JSON.stringify(x));
console.log(JSON.stringify(y));
res.send(`Body: "${x}" recieved in app.js! ------- Body: "${y}" recieved in app.js!`);
})
app.listen(4001, function() {
console.log("Server is listening on port 4001...");
})
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample Site</title>
<style>
body { padding-top: 50px; }
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>res.sendFile() Works!</h1>
<form method="POST" action="/submit-form", body= "key: 'hellow wolrd'"}>
<input type="submit">
</form>
</div>
</div>
</body>
</html>```
POST 请求的主体由表单中指定表单控件(input
、select
等元素)的值确定。
<form>
元素没有 body
属性。
MDN has a forms + express tutorial 我推荐你阅读。
<form method="POST" action="/submit-form">
<input name=key value="hello, world">
<button>Submit</button>
</form>