Django CSRF check failing with an Ajax POST request

前端 未结 22 1454
佛祖请我去吃肉
佛祖请我去吃肉 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:55

    If you use the $.ajax function, you can simply add the csrf token in the data body:

    $.ajax({
        data: {
            somedata: 'somedata',
            moredata: 'moredata',
            csrfmiddlewaretoken: '{{ csrf_token }}'
        },
    

提交回复
热议问题