django - axios Post 请求返回空响应
问题描述
我面临的问题是我发出的 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);
}
}
解决方案
请尝试像这样使用
import axios from "axios";
//... your codes here
axios({
url: "/users/",
method: "post",
data: formData,
headers: config.headers
})
.then(response => {
this.$router.push("/");
console.log(response);
})
.catch(error => console.error(error));
//... and other codes here
你可以看到更多的例子!
推荐阅读
- sql - 如何在单个查询中获取所有数据库及其各自用户列表的列表
- python-3.x - 如何使用 StringField 验证 Flask 表单中的电话号码?定义最小/最大长度不能限制用户输入文本
- android - 从视图缩小位图
- qt - 如何从 QML 中的父级访问内部项目?
- mongodb - MongoDB 根据objectId查找7天前创建的集合中的所有文档
- swiftui - Xcode 11 Beta 5 更新不分青红皂白地破坏了我的项目
- android - 有没有办法使用 Kotlin 将 CSV 文件导入 SQLite 数据库?
- android - AccountKit - 收到的消息包含(开发模式)
- php - 从php嵌入变量
- python - Sklearn 缩放:也缩放原始数据