axios POST 请求正在访问 Controller 上的 url,但为我的 POJO 类设置空值,当我在 chrome 中使用开发人员工具时,有效负载包含数据。我做错了什么?
Axios POST 请求:
var body = {
userName: 'Fred',
userEmail: 'Flintstone@gmail.com'
}
axios({
method: 'post',
url: '/addUser',
data: body
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
浏览器响应:
如果我将标题设置为:
headers:{
Content-Type:'multipart/form-data'
}
请求抛出错误
Error in posting multipart/form-data. Content-Type header is missing boundary
如果我在 postman 中发出相同的请求,它工作正常并为我的 POJO 类设置值。
任何人都可以解释如何设置边界或如何使用 axios 发送表单数据。
请您参考如下方法:
您可以使用 FormData() 发布 axios 数据像:
var bodyFormData = new FormData();
然后将字段添加到您要发送的表单中:
bodyFormData.append('userName', 'Fred');
如果您要上传图片,您可能需要使用.append
bodyFormData.append('image', imageFile);
然后就可以使用axios post方法了(可以相应修改)
axios({
method: "post",
url: "myurl",
data: bodyFormData,
headers: { "Content-Type": "multipart/form-data" },
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
相关 GitHub 问题:
Can't get a .post with 'Content-Type': 'multipart/form-data' to work @ axios/axios






