如何访问 express POST 请求中的数据?
How do I access the data in a POST request in express?
这是我的客户端代码:
$.post("/audio",
{
type: 'instrumental',
name: 'Instrumental_30SecondsToMars_TheKill'
},
function(data, status) {
alert("Data: " + data + "\nStatus: " + status);
});
这是我的服务器端代码:
app.post('/audio', function (req, res) {
console.log(req.body);
});
如何从服务器端函数中访问我在 post 中发送的类型和名称?
它肯定被调用,因为服务器是控制台日志记录。
您需要使用 bodyparser 中间件:
var bodyParser = require('body-parser');
然后
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
你需要使用像body-parser
这样的解析模块
这样使用:
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/audio', function (req, res) {
console.log(req.body); // <-- now has req.body.type, req.body.name
});
这是我的客户端代码:
$.post("/audio",
{
type: 'instrumental',
name: 'Instrumental_30SecondsToMars_TheKill'
},
function(data, status) {
alert("Data: " + data + "\nStatus: " + status);
});
这是我的服务器端代码:
app.post('/audio', function (req, res) {
console.log(req.body);
});
如何从服务器端函数中访问我在 post 中发送的类型和名称?
它肯定被调用,因为服务器是控制台日志记录。
您需要使用 bodyparser 中间件:
var bodyParser = require('body-parser');
然后
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
你需要使用像body-parser
这样的解析模块这样使用:
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/audio', function (req, res) {
console.log(req.body); // <-- now has req.body.type, req.body.name
});