0
我在下面有ajax代码,我想读取我使用ajax发送的数据,我想读取服务器端的数据。下面阅读博客req在节点js
$.ajax({
type: 'POST',
data: {hurl: "test data"},
contentType: 'application/json',
url: 'http://localhost:5000/hmap',
success: function(data) {
// console.log('success');
// console.log(JSON.stringify(data));
console.log(data);
}
});
});
是我的服务器端节点的js代码
var hmapdata = [];
app.post('/hmap', function(req, res) {
console.log(req.body);
MongoClient.connect(url, function(err, db) {
if (err)
throw err;
db.collection("heatmap").find().toArray(function(err, result) {
if (err)
throw err;
hmapdata = result;
db.close();
});
});
setTimeout(function() {
res.send(hmapdata);
}, 1000);
});
您req.body不包含你从Ajax请求发送数据。使用 –
使用开发工具中的“网络”选项卡收集关于POST调用的更多信息。 –
但问题是什么?如果您正在使用body-prser中间件,那么一切都应该工作 –