在django项目中通过Ajax发布时,csrf令牌丢失或不正确

时间:2013-10-23 17:25:20

标签: python ajax django

我请检查我使用以下js搜索内容。 这里我从name = csrfmiddlewaretoken获取csrf令牌。 请帮我吧

$(function(){
    $('#search').keyup(function(){
        $.ajax({
            type: "POST",
            url:"/article/search_title/",
            data:{
                'search_text' : $('#search').val(),
                'csrfmiddlewaretoken' : $('input[name=csrfmiddlewaretoken]').val() 
            },
            success: searchSuccess,
            dataType: 'html'

        });

    });

});

function searchSuccess() {
    $('search_success').html(data);

}

3 个答案:

答案 0 :(得分:0)

通过添加以下代码我绕过了csrf令牌问题 感谢

$(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'));
    }
});

答案 1 :(得分:0)

仅来自文档https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

// using jQuery
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;
}
var csrftoken = getCookie('csrftoken');

答案 2 :(得分:0)

我几天前写了一篇关于AngularJS和Django的帖子,其中我描述了让它们一起玩得很好的所有步骤:http://www.daveoncode.com/2013/10/17/how-to-make-angularjs-and-django-play-nice-together/

好读:)