axios - What are some reasons I could be getting a "Forbidden (CSRF cookie not set.)" error when trying to send a delete request?
问题描述
This is really confusing me because I know I'm setting the csrftoken correctly in the header. When I try and send the delete request on Postman I get the same error. I've been searching for a solution for hours and can't seem to find anything good! Do you know what could possibly be happening?
This is some relevant frontend code
var csrftoken = this.getCookie('csrftoken');
axios.defaults.headers.delete['X-CSRFToken'] = csrftoken
axios.defaults.headers.delete['X-Requested-With'] = 'XMLHttpRequest'
axios
.delete(`http://localhost:8000/api/todos/${item.id}`)
.then(res => this.refreshList());
Let me know if you need anymore info. Any help will be appreciated.
解决方案
我发现了问题。我的删除 URL 模式末尾有一个斜杠,正如您在此处看到的,在 axios 请求中的 ID 后面没有斜杠。程序员的经典错误。我讨厌这样的小虫子,不是吗?既然它已经消失了,仍然忍不住微笑。我想你只有通过经验才能学会检查这样的小事。这就是一个年轻的 Web 开发者的旅程。
推荐阅读
- c - 如何在内核空间中查找执行时间
- laravel - Laravel Jetstream 照片最大尺寸
- docker - 使用 nginx 在 docker 中托管时缺少 Blazor WASM 样式
- python - 将django中的图片上传到唯一文件夹
- c++ - 使用 std::tie() 和赋值的评估顺序
- microsoft-graph-api - 通过 Power Automate 添加的 AIP 敏感度标签
- excel - 使用 vba 为 A 列中的每个唯一值求和 B 列中的所有值
- xcode - 如何在文件检查器中编辑文件名?
- linked-server - 链接服务器和 Azure 托管实例
- javascript - P5.JS 动画 GIF 在渲染时显示所有帧