【问题标题】:Django CSRF check failing in JavaScript post request like a form submitDjango CSRF 检查在 JavaScript 发布请求中失败,如表单提交
【发布时间】:2016-10-18 17:34:37
【问题描述】:

我在这里使用答案 (JavaScript post request like a form submit) 在浏览器的 javascript 中发布。但是 Django CSRF 检查失败,因为模板中的表单中有 {% csrf_token %}。怎么做?我应该添加以下代码吗?

var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", 'csrfmiddlewaretoken');
hiddenField.setAttribute("value", '{{ csrf_token }}');
form.appendChild(hiddenField);

欢迎任何提示、建议和 cmets。 谢谢

【问题讨论】:

  • 您是在 javascript 中创建一个完整的表单并将其附加到您的模板中吗?

标签: javascript jquery django forms


【解决方案1】:

如果您使用 ajax 提交表单,您还必须提交 csrf 令牌值。 举个例子。

$.ajax({
    type: "POST",
    url: url,
    data:{
        'csrfmiddlewaretoken':$('input[name="csrfmiddlewaretoken"]').val(),
    },
    success: function(response){
    }
});

或者您可以将 ajax 数据中的序列化表单发送为

data: $("#form").serialize(),

【讨论】:

    【解决方案2】:

    我使用了 https://github.com/sigurdga/django-jquery-file-upload/blob/master/fileupload/static/js/csrf.js 的 getCookie() 函数和 JavaScript post request like a form submit 的 post() 函数。

    最终代码:

    // https://github.com/sigurdga/django-jquery-file-upload/blob/master/fileupload/static/js/csrf.js
    function getCookie(name) {
        console.log('getCookie');
        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;
                        }
                }
        }
        console.log('cookie:' + cookieValue);
        return cookieValue;
    }
    // https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit
    function post(path, params, method) {
            method = method || "post"; // Set method to post by default if not specified.
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);
            for(var key in params) {
                    if(params.hasOwnProperty(key)) {
                            var hiddenField = document.createElement("input");
                            hiddenField.setAttribute("type", "hidden");
                            hiddenField.setAttribute("name", key);
                            hiddenField.setAttribute("value", params[key]);
                            form.appendChild(hiddenField);
                    }
            }
            var hiddenField1 = document.createElement("input");
            hiddenField1.setAttribute("type", "hidden");
            hiddenField1.setAttribute("name", 'csrfmiddlewaretoken');
            hiddenField1.setAttribute("value", getCookie('csrftoken'));
            form.appendChild(hiddenField1);
    
            document.body.appendChild(form);
            form.submit();
    }
    

    欢迎任何 cmets。

    【讨论】:

    • 你是我看到的第一个使用 8 个空格缩进的人
    • @Endless 是有线的。我不知道为什么 8。我会弄清楚为什么。
    • @Endless 谢谢。复制和粘贴时我做错了。在我的新虚拟机上,我没有设置 .vimrc
    猜你喜欢
    • 2017-01-22
    • 2010-09-13
    • 2011-07-03
    • 2018-05-08
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多