首页 > 解决方案 > 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.

标签: axiosbackendhttp-deletecsrf-tokendjango-cors-headers

解决方案


我发现了问题。我的删除 URL 模式末尾有一个斜杠,正如您在此处看到的,在 axios 请求中的 ID 后面没有斜杠。程序员的经典错误。我讨厌这样的小虫子,不是吗?既然它已经消失了,仍然忍不住微笑。我想你只有通过经验才能学会检查这样的小事。这就是一个年轻的 Web 开发者的旅程。


推荐阅读