django - Django REST Framework 删除方法请求收到 Forbidden 403
问题描述
视图.py
class DeviceView(viewsets.ModelViewSet):
serializer_class = DevicesSerializer
queryset = Devices.objects.all()
permission_classes = [permissions.IsAuthenticated]
Axios 请求
axios.delete(`api/devices/${data.id}/`, {}, {
headers: {'X-CSRFToken': csrftoken }
})
.then((response) => {
console.log(response);
}).catch((error) => {
console.log(error);
});
当我在我的前端执行这个请求时,我得到一个响应:“DELETE http://localhost:3000/api/devices/4/ 403 (Forbidden)”。其中 4 是属于我要删除的记录的 ID。
我目前在 Django 中使用会话身份验证,并且我已将我的 CSRF 令牌值传递到我的请求标头中。
当我在表单上使用 PUT 和 POST 等其他方法时,它们可以正常工作。但是,不是删除
收到此错误我做错了什么?
解决方案
原来我只需要删除 Axios 请求中的空正文。
axios.delete(`api/devices/${data.id}/`, {
headers: {'X-CSRFToken': csrftoken }
})
.then((response) => {
console.log(response);
}).catch((error) => {
console.log(error);
});
推荐阅读
- react-native - 我想在反应本机应用程序上锁定屏幕并在屏幕加载之前检查密码
- c# - 是否“system.threading.channels”将解决跨进程应用程序的 pub sub 问题
- python - 传递带有空格和换行符的字符串作为命令行参数python
- python - 如何检查 Django Rest Framework 中每个请求中是否存在某些标头?
- javascript - 如何在 React js 中转换一个简单的 js 代码?
- awk - 如何通过使用awk忽略带有字符的行来找到该字段的最大值?
- angular - 我有这部分代码的解释吗?
- node.js - 网络错误:无法获取离子反应
- jsf - 由于 javax.faces.ViewState,无法从 Postman 加载网页?
- java - 使用 openapi-generator-gradle-plugin 只为 spring boot 生成 REST 接口