用React获取CSRFTOKEN Cookie

时间:2019-01-04 01:03:51

标签: reactjs django-rest-framework axios csrf-token

我正在使用 React 创建我的第一个应用程序,并与 Django Rest Framework 后端应用程序进行交互。他们俩都在本地服务器上运行。

我发现来自 React 前端的请求(通过 Axios 发送)要求一个 csrftoken cookie 。我阅读并关注了Django Documentation,但始终得到一个undefined csrftoken cookie

这是请求的代码:

handleClick() {
    var csrftokenCookie = Cookies.get('csrftoken');
    console.log(csrftokenCookie);

    const axios = require('axios');
    axios.post('http://127.0.0.1:8000/es/api-auth/login/', {
      next: '/',
      username: 'admin@funtastic.tours',
      password: 'funtadmin'
    })
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    })
  }

我打印以控制台Cookies.get('csrftoken')的结果,并且可以看到它始终是undefined

我不知道我要丢失什么才能获得 csrftoken cookie

0 个答案:

没有答案