0

嗨,请检查我正在使用下面的 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);

}
4

3 回答 3

0

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

好读:)

于 2013-10-24T10:13:09.357 回答
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'));
    }
});
于 2013-10-23T17:42:43.597 回答
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');
于 2013-10-24T09:59:18.133 回答