这JavaScript的代码
var params = 'username='+ nameValue +'&age=' + ageValue;
// 配置ajax对象
xhr.open('post', 'http://localhost:3000/post',true);
// 设置请求参数格式的类型(post请求必须要设置)
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
console.log(params)
// 发送请求
xhr.send(params);
// 获取服务器端响应的数据
xhr.onload = function () {
console.log(xhr.responseText)
}
这是服务器中的代码
app.post('/post', (req, res) => {
res.send(req.body);
});
服务器中显示{},什么都没传进来,但是在网页中查看post参数中的form data 是有值的!
试下JSON呢: