Django CREACT、axios和DRF的CSRF验证失败错误

Django CREACT、axios和DRF的CSRF验证失败错误,django,reactjs,rest,post,axios,Django,Reactjs,Rest,Post,Axios,我正试图发出一个类似这样的发帖请求 axios .post(`http://127.0.0.1:8000/api/create/${this.props.id}`, { headers: { Authorization: `Token ${token}` }, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-CSRFToken"

我正试图发出一个类似这样的发帖请求

axios
        .post(`http://127.0.0.1:8000/api/create/${this.props.id}`, {
          headers: {
            Authorization: `Token ${token}`
          },
          xsrfCookieName: "XSRF-TOKEN",
          xsrfHeaderName: "X-CSRFToken"
        })
        .then();
我还在settings.py中添加了一些基本内容,例如
CSRF\u COOKIE\u NAME=“XSRF-TOKEN”

我也有

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


    ),
}

您可能需要在代码中添加
确保\u csrf\u cookie

页面通过AJAX发出POST请求,并且该页面没有带有csrf_令牌的HTML表单,该令牌将导致发送所需的csrf cookie

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

阅读更多关于。如果有帮助,请告诉我。

我得到了失败的原因:CSRF cookie未设置可能的重复