Django 使用 Fetch 对 POST 请求返回 403 错误

2022-01-20 00:00:00 django csrf fetch jquery ajax

我有一个使用 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;
}

相关文章