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")
    }
});

猜你喜欢

转载自www.cnblogs.com/weilaibuxiangshuo/p/10981369.html