编程语言
首页 > 编程语言> > python – Firefox没有收到django csrf_token

python – Firefox没有收到django csrf_token

作者:互联网

我在django中提交ajax表单并使用

xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));

得到csrf_token.表格在镀铬方面运作良好.但是在firefox中,csrf_token的值为null,它给出403禁止错误.当我在控制台中检查cookie时,我没有在控制台中收到csrf_token.为什么django没有给firefox浏览器提供csrf_token?

解决方法:

将以下装饰器添加到生成包含表单的页面的视图中

@ensure_csrf_cookie

来自Django Docs –

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,django,firefox,django-csrf
来源: https://codeday.me/bug/20190623/1272736.html