众所周知前端向后台发送 post 请求时,必须验证 csrf
,不然会报错 403 Forbidden
。使用 Django Form 表单能够直接在表单里面添加 {% csrf_token %}
便可,要是经过 Ajax 发送请求又该怎么办?下面提供三种解决办法:javascript
<ul id="ddd"> <li>1</li> <li>2</li> <li>3</li> </ul> <button id="btn" type="button">Ajax 发送</button>
1. 方式一
<script src="{% static 'js/jquery-3.1.1.js' %}"></script> <script> $('#btn').click(function () { var li_content = []; $('#ddd').children('li').each(function () { li_content.push($(this).html()); }); console.log(li_content); $.ajax({ url: '/webs/test_json/', type: 'post', data: { 'li_list': JSON.stringify(li_content), csrfmiddlewaretoken: '{{ csrf_token }}' // 添加这句 }, success: function (arg) { console.log(arg); } }) }) </script>
2. 方式二
方式二仅在 Django 中适用,由于 {% csrf_token %}
是 Django 的模板语言,它会生成一个隐藏的 input
标签html
<ul id="ddd"> {% csrf_token %} <!--添加这句会生成一个隐藏的 input 标签,name='csrfmiddlewaretoken'--> <li>1</li> <li>2</li> <li>3</li> </ul> <button id="btn" type="button">Ajax 发送</button>
<script src="{% static 'js/jquery-3.1.1.js' %}"></script> <script> $('#btn').click(function () { var li_content = []; $('#ddd').children('li').each(function () { li_content.push($(this).html()); }); console.log(li_content); $.ajax({ url: '/webs/test_json/', type: 'post', data: { 'li_list': JSON.stringify(li_content), csrfmiddlewaretoken:$('[name="csrfmiddlewaretoken"]').val() // 添加这句,去获取 input 的值 }, success: function (arg) { console.log(arg); } }) }) </script>
3. 方式三
由于 cookie
中一样存在 csrftoken
,因此能够在 js
中获取:$.cooke("cstftoken")
,并将其添加到请求头中发送。前端
一、直接添加请求头:java
$.ajax({ url: '/webs/test_json/', headers: { "X-CSRFtoken":$.cookie("csrftoken")}, type: 'post', data: { 'li_list': JSON.stringify(li_content) }, success: function (arg) { console.log(arg); } }) })
二、若是页面有多个 Ajax,那么能够设置全局的:python
发送请求前会事先执行 $.ajaxSetup()
方法,它会从 cookie
中获取 csrftoken
jquery
$.ajaxSetup({ beforeSend: function (xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); } } }); $.ajax({ url: '/webs/test_json/', type: 'post', data: { 'li_list': JSON.stringify(li_content) }, success: function (arg) { console.log(arg); } })
三、若是想要实如今当 get
方式的时候不须要提交 csrftoken
,当 post
的时候须要,实现这种效果的代码以下:web
<script src="{% static 'js/jquery-3.1.1.js' %}"></script> <script type="text/javascript" src="/static/js/jquery.cookie.js"></script> <script> $('#btn').click(function () { var li_content = []; $('#ddd').children('li').each(function () { li_content.push($(this).html()); }); console.log(li_content); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } /* 全局Ajax中添加请求头X-CSRFToken,用于跨过CSRF验证 */ $.ajaxSetup({ beforeSend: function (xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); } } }); $.ajax({ url: '/webs/test_json/', type: 'post', data: { 'li_list': JSON.stringify(li_content) }, success: function (arg) { console.log(arg); } }) }) </script>
**Tips:**必定要导入
jquery.cookie.js
和jquery-3.3.1.js
文件 !!!ajax