Django Ajax CSRF错误403或500

时间:2011-06-01 17:22:22

标签: ajax django csrf django-csrf

我尝试更改管理界面。我添加了一个字段来通过Ajax更改我的商店应用程序中的订单状态。

我基于这个Django代码段 Admin list_display Ajax

这是我的JavaScript代码:

$(document).ready(function() {
    $('.order-helper').css("margin", "0");
    $('.order-helper').blur(function(){
        var input = $(this);
        $.ajax({
            url: "/ajax-status-update/"+ input.attr('name') +"/",
            data: order=input.attr('value'),
            type: "POST",
            complete: function(xhr_obj, msg){
                if (msg == 'success') {
                    input.css("border", "1px solid green");
                } else {
                    input.css("border", "1px solid red");
                }
            },
        });

    });
});

这是我的观点:

def order_helper(request, prod_id):
prod = get_object_or_404(FixedOrder, id=prod_id)
prod.status = request.POST.get('order')
prod.save()
return HttpResponse(content='Ok', status=200)

该视图不适用于Ajax调用。

我阅读了很多关于此的文章,对我来说没有任何作用。我知道它与CSRF以某种方式连接。在控制台中我收到403错误。

我读到我必须在任何地方添加此JavaScript代码段:

$(document).ajaxSend(function(event, xhr, settings) {
    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;
    }
    function sameOrigin(url) {
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

但如果我添加这个,我在控制台中得到500。我该如何检查它是什么类型的问题? 也许我错过了一个CSRF令牌?

1 个答案:

答案 0 :(得分:1)

data的{​​{1}}参数未正确转义。问题的核心可能就在那里:

$.ajax

如果您想继续使用CSRF中间件,您可能应该序列化一个表单:

$.ajax({
    url: "/ajax-status-update/"+ input.attr('name') +"/",
    data: "order=" + input.attr('value'),
    type: "POST",
    complete: function(xhr_obj, msg){
        if (msg == 'success') {
            input.css("border", "1px solid green");
        } else {
            input.css("border", "1px solid red");
        }
    },
});