我在 django 中提交一个 ajax 表单并使用
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
获取csrf_token。该表格在 chrome 中运行良好。但是在 firefox 中,csrf_token 的值为 null 并且它给出 403 forbidden 错误。当我在控制台中检查 cookie 时,我没有在控制台中收到 csrf_token。为什么 django 不将 csrf_token 提供给 firefox 浏览器?
最佳答案
将以下装饰器添加到生成包含表单的页面的 View
@ensure_csrf_cookie
来自 Django 文档 -
Page uses AJAX without any HTML form
A page makes a POST request via AJAX, and the page does not have an HTML form with a csrf_token that would cause the required CSRF cookie to be sent.
Solution: use ensure_csrf_cookie() on the view that sends the page.
关于python - Firefox 没有收到 django csrf_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105645/