【发布时间】:2017-02-21 10:19:44
【问题描述】:
编辑请参阅下面接受的答案以获取修复。我还必须从我的 POST 请求中删除行 contentType: 'appliction/json',。
我正在尝试向 Node.js / Express 发送一个字符串,但 req.body 是未定义的服务器端。
客户端 jQuery:
$.post({
traditional: true,
url: '/matches',
contentType: 'appliction/json',
data: viewedProfiles,
dataType: 'json',
success: function(response){
快递:
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.post('/matches', isLoggedIn, function(req, res){
console.log(req.body) // this is undefined
var loadedProfiles = []
loadedProfiles.push(req.body.viewedProfiles)
console.log('loadedProfiles')
console.log(loadedProfiles)
我试过了:
- 未指定“数据类型”
- 设置
data: JSON.stringify(viewProfiles) - 在客户端将字符串拆分成数组,然后让 jQuery 对其进行字符串化
- 寻找 req.params 而不是 req.body(抓着稻草)
我可以在开发工具中看到 XHR 请求,它包含我期望的字符串。
在将数据发送到 Express 时,我错过了什么特别明显的事情?
谢谢。
【问题讨论】:
-
您的页面中是否运行了
connect中间件? -
我没有,但我有 jQuery 数据帖子在应用程序的其他地方成功运行。
标签: javascript jquery node.js express post