nodejs request.body returns 空 object {}
nodejs request.body returns empty object {}
我正在使用以下代码,request.body returns {}
我希望我的输出为 {username:"Mani",password:"pass"}
如果此代码示例中有任何错误,请帮助我修复。
app.js
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
npm 版本:
表达 4.14.0
body-parser 1.15.2
方法:POST
header: Content-Type: application/json
请求负载:{用户名:"Mani",密码:"pass"}
app.js 控制台的输出是 {}
您的正文似乎是 json 文件。您需要配置 body-parser 以接受 json:
app.use(bodyParser.json());
试试这个,"content-type":"application/x-www-form-urlencoded",必须使用这个
// chnage content-type to content-type": "application/x-www-form-urlencoded
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:3000/",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded",
},
"data": {
"name": "mane"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我正在使用以下代码,request.body returns {}
我希望我的输出为 {username:"Mani",password:"pass"}
如果此代码示例中有任何错误,请帮助我修复。
app.js
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
npm 版本:
表达 4.14.0
body-parser 1.15.2
方法:POST
header: Content-Type: application/json
请求负载:{用户名:"Mani",密码:"pass"}
app.js 控制台的输出是 {}
您的正文似乎是 json 文件。您需要配置 body-parser 以接受 json:
app.use(bodyParser.json());
试试这个,"content-type":"application/x-www-form-urlencoded",必须使用这个
// chnage content-type to content-type": "application/x-www-form-urlencoded
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:3000/",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded",
},
"data": {
"name": "mane"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});