【发布时间】:2019-09-04 01:12:33
【问题描述】:
我遇到的问题是我发出的 axios POST 请求收到 NULL 响应。如何更正此错误?
我尝试将 multipart/form-data 更改为 x-www-form-urlencoded 和 multipart/x-www-form-urlencoded
async submitUser() {
this.memberObj = {
id: this.memberObjLocal.id,
name: this.memberObjLocal.name,
password: this.memberObjLocal.password,
email: this.memberObjLocal.email,
password: this.memberObjLocal.password,
gender: this.memberObjLocal.gender,
experience: this.memberObjLocal.experience,
birth: this.memberObjLocal.birth,
phoneNum: this.memberObjLocal.phoneNum,
address: this.address,
introduce: this.introduce,
pic: this.pic
};
const config = {
headers: { "Content-Type": "multipart/form-data" }
};
var formData = new FormData();
for (let data in this.memberObj) {
console.log(data);
formData.append(data, this.memberObj[data]);
console.log(this.memberObj[data]);
}
for (var key of formData.entries()) {
console.log(key[0] + ", " + key[1]);
}
try {
let response = await this.$axios.$post("/users/", formData, config);
this.$router.push("/");
console.log(response) // null
} catch (e) {
console.log(e.response);
}
}
【问题讨论】:
-
我已经确认了 formData 的值。
-
你检查过payload是否包含数据吗?
-
您是否收到错误或响应的正文为空?如果是后者,那么您的问题可能出在后端(或后端/前端不兼容) - 这个 axios POST 命中了什么?您在服务器日志中看到了什么?