【发布时间】:2018-11-19 07:31:45
【问题描述】:
我正在尝试在同一个多部分 POST 请求中向我的 REST 端点发送一个文件和一些 json。请求是使用 axios 库直接从 javascript 发出的,如下方法所示。
doAjaxPost() {
var formData = new FormData();
var file = document.querySelector('#file');
formData.append("file", file.files[0]);
formData.append("document", documentJson);
axios({
method: 'post',
url: 'http://192.168.1.69:8080/api/files',
data: formData,
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
}
但是,问题是当我在网络选项卡中检查 chrome 开发人员工具中的请求时,我发现 document 没有 Content-Type 字段,而 file 字段 Content-Type 是 application/pdf(我' m 发送一个 pdf 文件)。
在服务器上Content-Type 为document 是text/plain;charset=us-ascii。
更新:
我通过邮递员发送了一个正确的请求,将document 作为.json 文件发送。虽然我发现这只适用于 Linux/Mac。
【问题讨论】:
标签: javascript json file axios multipartform-data