Django as API + ReactJS - Redux: POST 使用 CSRF 令牌请求但仍未设置响应 CSRF 令牌
Django as API + ReactJS - Redux: POST request with CSRF token but still response CSRF Token not set
环顾四周后,我想确定自己做对了,但我开始怀疑,最糟糕的是:我 运行 没有选择/想法。
所以我将 django 用作 API 之类的(我只收到对某些资产的请求)除了基础 class 视图中的一个 POST 方法允许我用户下载文件。
问题是 django 期望我的 POST 上有一个 CSRF 令牌。
所以,这是我在我的 reactjs 中所做的:
export function sendData(endpoint, req, data) {
return dispatch => {
dispatch(requestData(data));
let csrfToken = Cookies.get('csrftoken');
return fetch(endpoint, {
method: req,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken,
},
body: JSON.stringify(data),
})
.then(checkStatus)
.then(reponse => {
console.log("Success!");
}).catch(err => {
err.response.json().then((json) =>{
let { Errors, Result } = json;
console.log('request failed: ', Errors, " ", Result);
});
});
};
};
如您所见,我正在使用 'whatwg-fetch' 库。
我试图用 X-CSRF-Token
替换 X-CSRFToken
但请求被阻止到 chrome "option" 并且似乎没有正确发送:
Request header field x-csrf-token is not allowed by Access-Control-Allow-Headers in preflight response.
但我仍然遇到我一直在阅读的错误:
CSRF verification failed. Request aborted.
Reason given for failure:
CSRF cookie not set.
呃。
我在这里错过了什么?
在我看来,我已经尝试了所有类型的装饰器,即使是这样:
class DownloadAssetsView(ViewUrlMixin, ListView):
@csrf_exempt
def post(self, request, *args, **kwargs):
print(request)
return HttpResponse("coucou", status=200, content_type='application/json')
但我做不到..
PS: django 根本没有向我的客户端呈现模板。
我们在 project.the 阻止未知令牌的 csrf 令牌中遇到了这个问题,request.if 你看到了关于 csrf 令牌的文档,你可以找到 that.the csrf 令牌阻止了你的外部请求到你的 django server.we 将反应项目合并到我们的 django.it fixed.try .im 不是这方面的专家,但你可以检查一下。
环顾四周后,我想确定自己做对了,但我开始怀疑,最糟糕的是:我 运行 没有选择/想法。
所以我将 django 用作 API 之类的(我只收到对某些资产的请求)除了基础 class 视图中的一个 POST 方法允许我用户下载文件。
问题是 django 期望我的 POST 上有一个 CSRF 令牌。
所以,这是我在我的 reactjs 中所做的:
export function sendData(endpoint, req, data) {
return dispatch => {
dispatch(requestData(data));
let csrfToken = Cookies.get('csrftoken');
return fetch(endpoint, {
method: req,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken,
},
body: JSON.stringify(data),
})
.then(checkStatus)
.then(reponse => {
console.log("Success!");
}).catch(err => {
err.response.json().then((json) =>{
let { Errors, Result } = json;
console.log('request failed: ', Errors, " ", Result);
});
});
};
};
如您所见,我正在使用 'whatwg-fetch' 库。
我试图用 X-CSRF-Token
替换 X-CSRFToken
但请求被阻止到 chrome "option" 并且似乎没有正确发送:
Request header field x-csrf-token is not allowed by Access-Control-Allow-Headers in preflight response.
但我仍然遇到我一直在阅读的错误:
CSRF verification failed. Request aborted. Reason given for failure: CSRF cookie not set.
呃。
我在这里错过了什么?
在我看来,我已经尝试了所有类型的装饰器,即使是这样:
class DownloadAssetsView(ViewUrlMixin, ListView):
@csrf_exempt
def post(self, request, *args, **kwargs):
print(request)
return HttpResponse("coucou", status=200, content_type='application/json')
但我做不到..
PS: django 根本没有向我的客户端呈现模板。
我们在 project.the 阻止未知令牌的 csrf 令牌中遇到了这个问题,request.if 你看到了关于 csrf 令牌的文档,你可以找到 that.the csrf 令牌阻止了你的外部请求到你的 django server.we 将反应项目合并到我们的 django.it fixed.try .im 不是这方面的专家,但你可以检查一下。