<i id='19Lk7'><tr id='19Lk7'><dt id='19Lk7'><q id='19Lk7'><span id='19Lk7'><b id='19Lk7'><form id='19Lk7'><ins id='19Lk7'></ins><ul id='19Lk7'></ul><sub id='19Lk7'></sub></form><legend id='19Lk7'></legend><bdo id='19Lk7'><pre id='19Lk7'><center id='19Lk7'></center></pre></bdo></b><th id='19Lk7'></th></span></q></dt></tr></i><div id='19Lk7'><tfoot id='19Lk7'></tfoot><dl id='19Lk7'><fieldset id='19Lk7'></fieldset></dl></div>

    <small id='19Lk7'></small><noframes id='19Lk7'>

      <bdo id='19Lk7'></bdo><ul id='19Lk7'></ul>
  1. <tfoot id='19Lk7'></tfoot>

    1. <legend id='19Lk7'><style id='19Lk7'><dir id='19Lk7'><q id='19Lk7'></q></dir></style></legend>

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

      Django returns 403 error on POST request with Fetch(Django 使用 Fetch 对 POST 请求返回 403 错误)

        <tfoot id='ABbZb'></tfoot>

        <legend id='ABbZb'><style id='ABbZb'><dir id='ABbZb'><q id='ABbZb'></q></dir></style></legend>

          <tbody id='ABbZb'></tbody>
        • <i id='ABbZb'><tr id='ABbZb'><dt id='ABbZb'><q id='ABbZb'><span id='ABbZb'><b id='ABbZb'><form id='ABbZb'><ins id='ABbZb'></ins><ul id='ABbZb'></ul><sub id='ABbZb'></sub></form><legend id='ABbZb'></legend><bdo id='ABbZb'><pre id='ABbZb'><center id='ABbZb'></center></pre></bdo></b><th id='ABbZb'></th></span></q></dt></tr></i><div id='ABbZb'><tfoot id='ABbZb'></tfoot><dl id='ABbZb'><fieldset id='ABbZb'></fieldset></dl></div>
            • <bdo id='ABbZb'></bdo><ul id='ABbZb'></ul>

                <small id='ABbZb'></small><noframes id='ABbZb'>

                本文介绍了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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Difference between fetch, ajax, and xhr(fetch、ajax 和 xhr 之间的区别)
                How to use JSONP on fetch/axios cross-site requests(如何在 fetch/axios 跨站请求中使用 JSONP)
                Open page in new window without popup blocking(在新窗口中打开页面而不阻止弹出窗口)
                how to clone content of a div to another div(如何将一个div的内容克隆到另一个div)
                Remove Styles from Text when Copying / Cutting using CSS or Javascript(使用 CSS 或 Javascript 复制/剪切时从文本中删除样式)
                  <bdo id='jy3ch'></bdo><ul id='jy3ch'></ul>

                    <tfoot id='jy3ch'></tfoot>
                      <tbody id='jy3ch'></tbody>
                        1. <i id='jy3ch'><tr id='jy3ch'><dt id='jy3ch'><q id='jy3ch'><span id='jy3ch'><b id='jy3ch'><form id='jy3ch'><ins id='jy3ch'></ins><ul id='jy3ch'></ul><sub id='jy3ch'></sub></form><legend id='jy3ch'></legend><bdo id='jy3ch'><pre id='jy3ch'><center id='jy3ch'></center></pre></bdo></b><th id='jy3ch'></th></span></q></dt></tr></i><div id='jy3ch'><tfoot id='jy3ch'></tfoot><dl id='jy3ch'><fieldset id='jy3ch'></fieldset></dl></div>

                        2. <small id='jy3ch'></small><noframes id='jy3ch'>

                        3. <legend id='jy3ch'><style id='jy3ch'><dir id='jy3ch'><q id='jy3ch'></q></dir></style></legend>