django - 在 reactjs 中使用 axios 发出请求时出现 400(错误请求)状态码
问题描述
当尝试在 reactjs 中使用 axios 时,它正在向邮递员发出请求,它显示 400 状态代码
我已经尝试过 - 添加删除标题 - 添加边界:--foo
handleSubmit(event) {
let data = new FormData();
data.append("username", this.state.username)
data.append("password", this.state.password)
data.append("confirm_password", this.state.confirm_password)
data.append("email", this.state.email)
event.preventDefault();
axios({
method: 'post',
url: 'http://localhost:8000/api/account/register/',
data: data,
headers:{
"Content-Type":"application/x-www-form-urlencoded; boundary:XXboundaryXX"
}
})
.then((response) => {
console.log('bitchhh');
})
.catch((response) => {
//handle error
console.log(response);
});
}
解决方案
如果数据有任何错误,rest framework 不会返回 2xx 状态码,它总是返回 400 bad request。这意味着您发送的请求中有错误。
邮递员也收到 400 错误请求,但它显示响应数据(错误消息)。
axios 将 400 状态代码视为错误,因此您必须在 catch 块中处理它。如果您想访问发送错误的响应,您可以从
.catch((err) => {
//handle error
console.log(err.response.data);
});
推荐阅读
- numpy - 在 Numpy 二维数组中移动行的位置
- javascript - 包括 AngularJS 和 ASP.NET MVC
- node.js - 用于 socket-io 适配器的到 Redis 实例的 SSL 连接
- css - CSS flexbox 和 flex 1 的旋转输入范围
- python - 如何在 Odoo 中安装硒?
- linux - 有没有办法将参数传递给 .bin 文件的执行?
- pandas - 过去 n 天的滚动计数
- node.js - TypeScript 似乎没有检查类实例属性的类型
- go - 打开文件失败:解析时出现意外错误:读取数据失败。父错误:binary.Read:无效类型 uint32
- android - 如何将我从图库中挑选的照片设置为纵向?