我想通过Node.js中的express和body-parser向服务器发送带有自定义字符串值的请求,但是当我尝试检查发布价值。节点js请求正文类型是[对象,对象]
[对象的对象]
服务器 -
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/', callback)
function callback(req, res) {
console.log('post/' + req.body)
res.send('post success!')
}
客户端 -
var request = require('request')
request.post({
url: 'http://127.0.0.1:8000/',
body: 'testing'
}, function optionalCallback (err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err)
}
console.log('Upload successful! Server responded with:', body)
})
客户端日志 -
上传成功!服务器回应:发布成功!
服务器日志 -
后/ [目标对象]
我怎样才能得到字符串内容"testing"
呢? 谢谢!
将'toString()'从'req.body'中移除。 – gforce301
尝试使用'JSON.stringify',或'console.dir'而不是'.log'? –