django中csrf_token处理方式

第一:在HTML中加入{% csrf_token %}

$.ajax({
    url: '{% url "ceshi:list" %}',
    type: 'post',
    dataType: 'json',
    cache: false,
    data: {
        key: "11111",
        csrfmiddlewaretoken: $('[name="csrfmiddlewaretoken"]').val()
},

success: function(obj)
{
    console.log("hello")
}
});

第二:先在HTML中加入{% csrf_token %}

$.ajax({
    url: '{% url "ceshi:list" %}',
    type: 'post',
    dataType: 'json',
    cache: false,
    headers: {"X-CSRFToken": $('[name="csrfmiddlewaretoken"]').val()},
    data: {
        key: "11111",
    },

    success: function(obj)
    {
        console.log("hello")
    }
});

 

posted @ 2019-06-05 19:00  代码家园  阅读(1649)  评论(0编辑  收藏  举报