【发布时间】:2018-02-08 11:52:10
【问题描述】:
当我想通过 http.post 将一些数据从我的 angular 客户端传递到我的 node.js 服务器时,我遇到了一个问题。
事情就是这样,使用 JSON.stringify(my text) 传递文本可以正常工作,但鉴于我想传递一个文件 + 我的文本,我想使用 formData。
当我尝试在服务器端取回数据时,我的 req.body 为空,我无法检索数据。
这是我的客户端代码:
[...]
var formData = new FormData();
formData.append('name', product.name);
formData.append('benefits_detail', product.benefits_detail);
formData.append('sections', product.sections);
formData.append('image', product.image); // image is my file
return this.http.post('http://localhost:3000/product', formData, {headers: headers}).map(........)
然后是我的服务器,我尝试取回我的数据:
router.post('/', function (req, res, next) {
console.log('req.body');
console.log(req.body);
console.log(req.body.formData);
...
这里的 console.log 是空的,就像显示:{}
有人可以帮忙吗?
谢谢你
【问题讨论】: