jquery django set global token

JQUEYR by the ajaxSetup, django set of token, i.e., do not need to always use the reference;

first step:

Django first set of html

{%  csrf_token %}

Step two:

A new js file reads as follows

function Token(){
    return $('[name="csrfmiddlewaretoken"]').val();
}

  $.ajaxSetup({
        cache: false,
        beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", Token());
        }
    });

third step:

Introducing django page, which js, as can POST, token required do not need to reference

<script src="{% static 'js/jquery.min.js' %}" charset="utf-8"></script>
 
<script src="{% static 'js/ajaxSet.js' %}" charset="utf-8"></script>

 

Guess you like

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