BodyParser 给出一个空对象
BodyParser is giving an empty object
我正在尝试处理 post 路由器。我正在使用 postman 使 req.body 成为一个空对象 {}
请查看我的代码...我知道这已被问过很多次。但我找不到适合自己的解决方案
const express = require('express');
const bodyParser = require('body-parser');
const app =express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.post('/register', (req, res)=>{
res.send(req.body);
});
通常,请求应使用以下三种内容类型之一发送:原始、json 和文本。
尝试发送请求:内容类型:"application/json; charset=utf-8"
对于邮递员,确保你有这两个 Content-Type
headers 集之一:
application/json
:
You have to select the JSON
option from the dropdown
x-www-form-urlencoded
:
Since you've setup bodyParser to also support urlencoded queries
如果这不是问题所在,请编辑您的问题以包含 the cURL
version of your request,以获得更有帮助的答案。
现在,在进入 app.js
之前,您首先需要打印正在进入对象或未进入对象的请求输入,以便首先在 EJS 文件中正确调整表单,
<form action="/yourRoute" method="POST">
<input type="text" name="newFriend" placeholder="Name">``
<button>You just made new friend!</button>
</form>
注意:不要忘记在输入中添加名称,即 name="newFriend"
完成此操作后,将给定代码输入您的 app.js
文件
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
然后 运行 您的代码并注意您的提示!
我正在尝试处理 post 路由器。我正在使用 postman 使 req.body 成为一个空对象 {} 请查看我的代码...我知道这已被问过很多次。但我找不到适合自己的解决方案
const express = require('express');
const bodyParser = require('body-parser');
const app =express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.post('/register', (req, res)=>{
res.send(req.body);
});
通常,请求应使用以下三种内容类型之一发送:原始、json 和文本。
尝试发送请求:内容类型:"application/json; charset=utf-8"
对于邮递员,确保你有这两个 Content-Type
headers 集之一:
application/json
: You have to select theJSON
option from the dropdownx-www-form-urlencoded
: Since you've setup bodyParser to also support urlencoded queries
如果这不是问题所在,请编辑您的问题以包含 the cURL
version of your request,以获得更有帮助的答案。
现在,在进入 app.js
之前,您首先需要打印正在进入对象或未进入对象的请求输入,以便首先在 EJS 文件中正确调整表单,
<form action="/yourRoute" method="POST">
<input type="text" name="newFriend" placeholder="Name">``
<button>You just made new friend!</button>
</form>
注意:不要忘记在输入中添加名称,即 name="newFriend"
完成此操作后,将给定代码输入您的 app.js
文件
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
然后 运行 您的代码并注意您的提示!