简体   繁体   English

CSRF验证失败,并带有react,axios和DRF错误

[英]CSRF verification failed error with react, axios and DRF

I am trying to make a post request which looks like this 我正在尝试发出类似以下的帖子请求

axios
        .post(`http://127.0.0.1:8000/api/create/${this.props.id}`, {
          headers: {
            Authorization: `Token ${token}`
          },
          xsrfCookieName: "XSRF-TOKEN",
          xsrfHeaderName: "X-CSRFToken"
        })
        .then();

I have added essential things in settings.py also, such as CSRF_COOKIE_NAME = "XSRF-TOKEN" 我还在settings.py中添加了一些基本内容,例如CSRF_COOKIE_NAME = "XSRF-TOKEN"

I also have 我也有

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',


    ),
}

You may need to add ensure_csrf_cookie in your code. 您可能需要在代码中添加ensure_csrf_cookie

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. 页面通过AJAX发出POST请求,并且该页面没有带有csrf_token的HTML表单,该表单会导致发送所需的CSRF cookie。

from django.views.decorators.csrf import ensure_csrf_cookie
 @ensure_csrf_cookie

Read more about ensure_csrf_cookie . 阅读更多有关sure_csrf_cookie的信息 Let me know if that helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM