Django returns POST 请求 Fetch 时出现 403 错误

Django returns 403 error on POST request with Fetch

我有一个使用 graphene-django 实现的 graphql 服务器。我可以像这样使用 jquery 对其进行查询:

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。我在 jQuery 中通过在调用之前添加 ajaxSetup... 解决了同样的问题。

这是使用 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 the docs say,再次失败。我尝试使用 credentials: 'same-origin' 并以不同方式组合此选项,同样的结果。网络对此异常安静,我做错了什么?

解决方案在 getCookie() 方法中。

  fetch("/graphql", {
        method: "POST",
        credentials: "same-origin",
        headers: {
          "X-CSRFToken": getCookie("csrftoken"),
          "Accept": "application/json",
          'Content-Type': 'application/json'
        },
        body:JSON.stringify(query)
      })

当然,方法必须在同一页面上。摘自 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;
}

如果你想让那个 post 请求来自不同的域(如果应用程序的前端是 React 或 angular 而后端是 Django),请确保在设置文件中添加以下内容:

  1. 更新 INSTALLED_APPS 以使用 'coreHeaders' :

    INSTALLED_APPS = [
    'corsheaders',
    ]

  2. 通过再次将以下内容添加到设置文件来将您的前端域列入白名单:

    CORS_ORIGIN_WHITELIST = ( 'localhost:8080', )

现在允许为任何人发出此 post 请求的权限:

注意:应该在不需要对用户进行身份验证的情况下使用 posting 我们服务器上的任何内容,例如,当新用户注册第一个时时间

from rest_framework.permissions import AllowAny

class CreateUser(APIView):
    permission_classes = (AllowAny,)
    def post(self, request, format=None):
        return(Response("hi"))