M. *_*. H 0 multipartform-data node.js express vue.js axios
我送一个FormData从VueJS应用程序中使用Axios。问题是当我输出FormData它时它是空的。我之前在发送文件时使用过相同的方法(我现在不发送文件),然后FormData显示我附加到它的正确数据。我附加的数据是字符串类型。
客户端
onUpload(): void {
const fd = new FormData();
fd.append("id", this.chosenRoute.id);
fd.append("name", this.routeName);
fd.append("description", this.routeDescription);
fd.append("activity", this.activity);
fd.append("preamble", this.preamble);
axios.post('http://localhost:8080/editRoute', fd, {
onUploadProgress: uploadEvent => {
console.log('Upload Progress' + Math.round(uploadEvent.loaded / uploadEvent.total) * 100 + " %");
}
}).then(
res => {
console.log(res);
});
}
Run Code Online (Sandbox Code Playgroud)
服务器端
app.post('/editRoute', function(req,res){
console.log(req.body);
const routeId = req.body.id;
console.log(routeId);
Route.findById(routeId, (err, route) => {
res.set("Access-Control-Allow-Origin", "*");
route.update(req.body);
});
});
Run Code Online (Sandbox Code Playgroud)
来自 axios 文档:
默认情况下,axios 将 JavaScript 对象序列化为 JSON。
因此,您不能将 JSFormData对象直接传递给axios调用的选项。如果必须使用FormData,请参阅此处推荐的方法:https : //www.npmjs.com/package/axios#using-applicationx-www-form-urlencoded-format
但是,如果,正如您在上面提到的,但看起来像键/值对,则根本不要使用FormData,而是使用常规的 JavaScript 对象。
const body = {
"id": this.chosenRoute.id.
"name": this.routeName,
"description": this.routeDescription,
"activity": this.activity,
"preamble": this.preamble
}
axios.post('http://localhost:8080/editRoute', body, ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7049 次 |
| 最近记录: |