我知道这已被多次询问,但我一直在四处寻找,但仍无法找到问题的答案.
这是我的代码,我确保在定义路由之前使用和配置主体解析器.我只使用.json()和bodyParser,因为我现在只测试一个POST函数,但我甚至尝试过app.use(bodyParser.urlencoded({extended:true}));
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
});
app.post('/itemSearch', function(req, res) {
//var Keywords = req.body.Keywords;
console.log("Yoooooo");
console.log(req.headers);
console.log(req.body);
res.status(200).send("yay");
});
Run Code Online (Sandbox Code Playgroud)
这是我收到的回复
Node app is running at localhost:5000
Yoooooo
{ host: 'localhost:5000',
connection: 'keep-alive',
'content-length': '146',
'cache-control': 'no-cache',
origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc',
accept: '*/*', …Run Code Online (Sandbox Code Playgroud) 我正在向后端使用一个简单的发布请求来获取表单数据,并且由于某种原因,正文始终为空。我试图隔离这个问题,所以我将内容类型更改为应用程序 json,并将数据更改为 json,只有这样我才能发送数据。
客户端:
submitForm(event) {
event.preventDefault();
console.log("gggg");
const data = new FormData(event.target);
axios.post("http://localhost:4000/user-form-post",data).then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
服务器端:
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended:true}));
app.use(express.urlencoded());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.use(logger('dev'));
app.post('/user-form-post', (req,res) =>{
console.log("dfdf");
console.log(req.body); // alwayes print empty dict {}
res.end();
})
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它需要 jsons(预期行为):
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended:true}));
Run Code Online (Sandbox Code Playgroud)
与邮差的行为相同。
我已经尝试了许多 StackOverflow 答案,这种方法通常使用 body-parser 工作,但是我一直在req.body使用 AJAX 或表单数据获取任何输出时遇到问题。
在server.js:
app.use(helmet()); // Helmet middleware
app.use('/assets', express.static('resources/web/assets')); // Makes /assets public
app.use(require('./resources/modules/session.js')); // Custom session middleware
app.set('view engine', 'ejs'); // Sets EJS to the view engine
app.set('views', `${__dirname}/resources/web/pages`); // Sets the views folder
app.use(cookieParser()); // cookie-parser middleware
app.use(bodyParser.urlencoded({ extended: true })); // body-parser's middleware to handle encoded data
app.use(bodyParser.json()); // body-parser's middleware to handle JSON
app.use(fileUpload({ limits: { fileSize: 100 * 1024 * 1024 } })); // express-fileupload middleware …Run Code Online (Sandbox Code Playgroud) 我刚刚安装了最新版本的模块.我无法获得任何GET或POST变量.我做错了什么?节点:v0.12.2
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
});
app.listen(3000,function(){
console.log("Started on PORT 3000");
})
Run Code Online (Sandbox Code Playgroud)
http:// localhost:3000 /?token = devvvvv GET返回:你发布了:{}
谢谢你的答案,但POST的问题没有解决...在http:// localhost:3000上的POST令牌= as123ds /在req.body中返回空数组我该如何解决这个问题?
我有以下表单并提交结果为空req.body这是我的HTML:
<form action="/contact" name="contactUs" id="contactUs" method="POST">
<label for="email">Email Message</label>
<input type="text" name="email" id="email">
<input type="submit" value='Save'>
</form>
Run Code Online (Sandbox Code Playgroud)
这是我的(部分)server.js:
app.use(bodyParser.json());
app.use("/", expressStaticGzip("dist"));
app.post("/contact", (req, res) => {
console.log('anyone there?')
res.json(req.body);
});
Run Code Online (Sandbox Code Playgroud)
该console.log工程的罚款.我得到一个空的对象req.body.
我的 Web 应用程序在后端使用 Node.js 和 Express。当违反内容安全策略 (CSP) 时,报告 URI 报告空对象。我的后台代码如下:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(helmet({
contentSecurityPolicy: {
directives: {
// some policies here
reportUri: ["/my_amazing_csp_report_parser"],
},
},
}));
app.use('/my_amazing_csp_report_parser', (req, res, next) => {
console.log(req.body);
next();
})
Run Code Online (Sandbox Code Playgroud)
我在MDN 的文档中读到,报告 URI 应报告整个 JSON 对象。但我的console.log(req.body);返回一个空对象。我想知道我用app.use(bodyParser.urlencoded({ extended: true }));and解析 JSON 对象是否错误app.use(bodyParser.json());?
服务器:
//Body Parser
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
Run Code Online (Sandbox Code Playgroud)
客户:
<form enctype="multipart/form-data" method="post" action="../users/editProfile" class="uploadForm">
Run Code Online (Sandbox Code Playgroud)
出于某种原因,req.body我提交表单时总是空的.它之前的工作非常好,据我所知,我添加的唯一内容是9个输入,总共15个.
可能是什么导致了这个?
我已经检查了这个:req.body在帖子上是空的
这些答案似乎都没有解决我的问题.
express ×6
node.js ×6
body-parser ×4
javascript ×3
backend ×1
client ×1
form-data ×1
forms ×1
post ×1
postman ×1