我怎样才能得到饼干csrftoken
in Axios.interceptors.request
的配置?
Axios.interceptors.request.use(
config => {
if (
config.method === "post" ||
config.method === "put" ||
config.method === "delete"||
config.method === "get"
) {
}
if (Cookies.get('token')!==undefined) {
config.headers['Authorization']= 'Token '+Cookies.get('token');
}
// there I try to get the `csrftoken` in the Cookies, but I can not get.
if (Cookies.get('csrftoken')!==undefined) {
config.headers['x-csrftoken']= Cookies.get('csrftoken'); // 'CSRFToken'
}
return config;
},
error => {
return Promise.reject(error.data.error.message);
}
);
In the Axios.interceptors.request
的配置我无法获取 Cookiescsrftoken
: Cookies.get('csrftoken')
.
我的 AxiosConfig 代码如下:
AxiosConfig:{
baseURL: 'http://10.10.10.105:8001/',
responseType: "json",
withCredentials: true, // there will send the Cookie (with it there are: sessionid, csrftoken)
xsrfCookieName: 'csrftoken', // default: XSRF-TOKEN
xsrfHeaderName: 'x-csrftoken', // default: X-XSRF-TOKEN
headers: {
"Content-Type": "application/json;charset=utf-8"
}
}
edit-1
有的是csrftoken
在 Cookie 中。
edit-2
而在cookie中,没有csrftoken
too.
edit-3
如果我得到document.cookie
在控制台中,我会得到""
:
```
文档.cookie
edit-4
在我的 Django 后端中,settings.py
:
已安装的应用程序:
...
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_docs',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
...
我不确定是否rest_auth
and allauth
将影响csrftoken
.