[英]How can I make a POST-request using Javscript in Django (CSRF-Token)?
This question has been asked before, but those solutions didn't work for me that well:之前有人问过这个问题,但这些解决方案对我来说并不那么好:
Fetch, set-cookies and csrf 获取、设置 cookie 和 csrf
Proper Django CSRF validation using fetch post request 使用获取发布请求进行正确的 Django CSRF 验证
Right now the content of my post request is correct and being, but the 403 error still shows up.现在我的帖子请求的内容是正确的并且是正确的,但是仍然出现 403 错误。
const url = "/post/create"
let csrftoken = Cookies.get('csrftoken'); //using library
const headers = new Headers({
'X-CSRF-TOKEN': csrftoken
});
return fetch(url, {
method: 'POST',
headers,
credentials: 'same-origin',
mode: 'same-origin',
body: JSON.stringify({
content: content
})
});
Do you have any ideas?你有什么想法?
EDIT: This is the solution I found编辑:这是我找到的解决方案
fetch(url, {
method: 'POST',
mode: "same-origin",
headers: {
"X-CSRFToken": csrftoken,
"Accept": "network/json",
"Content-Type": "network/json",
},
body: JSON.stringify({
content: content
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.