在代码中,可以通过以下方式设置 Content-Type 为 multipart/form-data:
const formData = new FormData();
formData.append('image', file);
axios.post('/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
其中,FormData
是用于创建表单数据的内置对象,可通过 FormData.append()
方法向表单中添加数据。在axios请求中,通过设置headers对象中的 Content-Type
值为 multipart/form-data 来告诉服务端请求的数据格式。
使用上述代码解决后,即可确保 Axios post 请求的 Content-Type 被正确设置为 multipart/form-data,服务端能够正确识别请求中的数据格式。