Django CSRF check failing with an Ajax POST request

前端 未结 22 1710
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:46

I could use some help complying with Django\'s CSRF protection mechanism via my AJAX post. I\'ve followed the directions here:

http://docs.djangoproject.com/en/dev/r

22条回答
  •  离开以前
    2020-11-22 03:50

    Related to the chosen Answer, just want to add on to the chosen Answer.

    In that answer, regarding the solution with .ajaxSetup(...). In your Django settings.py, if you have

    CSRF_USE_SESSIONS = True
    

    It would cause the chosen Answer to not work at all. Deleting that line, or setting it to False worked for me while implementing the chosen Answer's solution.

    Interestingly, if you set the following in your Django settings.py

    CSRF_COOKIE_HTTPONLY = True
    

    This variable will not cause the chosen Answer's solution to stop functioning.

    Both CSRF_USE_SESSIONS and CSRF_COOKIE_HTTPONLY comes from this official Django doc https://docs.djangoproject.com/en/2.2/ref/csrf/

    (I do not have enough rep to comment, so I am posting my inputs an Answer)

提交回复
热议问题