In csrf_token treatment django

First: first added in 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")
}
});

Second: to join {% csrf_token%} in HTML

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

 

Guess you like

Origin www.cnblogs.com/weilaibuxiangshuo/p/10981369.html