Django returns 403 error on POST request with Fetch(Django 使用 Fetch 对 POST 请求返回 403 错误)
问题描述
我有一个使用 graphene-django 实现的 graphql 服务器.我可以像这样使用 jquery 对其进行查询:
I have a graphql server implemented using graphene-django. I can make queries to it using jquery like this:
function allIngredients() {
return 'query{allProducts{edges{node{name}}}}'
}
var query = allIngredients();
$.ajaxSetup({
data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
});
$.post("/graphql", {query: query}, function(response) {
console.log(response);
})
但是,当我尝试使用 Fetch 进行此调用时,由于 CORS 问题,我得到了 403.我通过在调用之前添加 ajaxSetup... 在 jQuery 中解决了同样的问题.
However, when I try this call with Fetch, I get a 403, because of the CORS issue. I solved the same problem in jQuery by adding ajaxSetup... before the call.
这是使用 fetch 的调用:
Here's the call using fetch:
fetch('/graphql', {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({
csrfmiddlewaretoken: '{{ csrf_token }}',
query:`{allProducts{
edges{
node{
id
name
orderPrice
sellPrice
}
}
}`})
}
)
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
})
我尝试以与在 jQuery 示例中类似的方式将 csrfmiddlewaretoken 添加到正文中,但没有运气.我尝试添加凭据:'include' as 文档说,再次没有运气.我尝试使用 credentials: 'same-origin' 并以不同方式组合此选项,再次获得相同的结果.网络对此异常安静,我做错了什么?
I tried adding csrfmiddlewaretoken to the body in the similar way as I did in jQuery example, no luck. I tried adding credentials: 'include' as the docs say, again no luck. I tried with credentials: 'same-origin' and combining this options differently, again the same result. Web is unusually quiet about this, what am I doing wrong?
推荐答案
解决方案在 getCookie() 方法中.
The solution was in the getCookie() method.
fetch("/graphql", {
method: "POST",
credentials: "same-origin",
headers: {
"X-CSRFToken": getCookie("csrftoken"),
"Accept": "application/json",
'Content-Type': 'application/json'
},
body:JSON.stringify(query)
})
当然,方法必须在同一页面上.取自 Django 文档.
And of course the method has to be on the same page. Taken from Django Docs.
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
这篇关于Django 使用 Fetch 对 POST 请求返回 403 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 使用 Fetch 对 POST 请求返回 403 错误


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01