When the form form is submitted as a post, Django defaults to a validation mechanism CSRF validation
<form action="/day02/login/"Method="Post"> {% Csrf_token%} user name:<input type="text"Name="User">Password<input type="text"Name="pwd"> <input type="checkbox"Name="Deng"Value="1">10 sec Free Login<input type="Submit"Value="Submit"> <input type="Button"Value="Click"Id="btn"></form>
Random cookie validation must be sent in order for the Django mechanism to verify success
If you submit by using Ajax Post, 403 Forbidden verification will appear.
Workaround: Send the request header in the submission with a cookie variable, the request head cannot be underlined (note)
$.ajax ({URL:'/day02/login/', type:'POST', data:{'User':'Keke','pwd':'123'}, headers:{'X-csrftoken': $.cookie ('Csrftoken')}, Suceess:function (ARG) {}
If more than one AJAX trigger, each request a little bit of trouble, in Ajax there is a pre-trigger mechanism.
$.ajaxsetup ({ beforesend:function (xhr,settings) { xhr.setrequestheader (' X-csrftoken', $.cookie ('csrftoken'); } );
Validation commits prior to Ajax. The parameters are fixed.
But the downside to this is that all Ajax, whether it's get or post, or any other request, will go through Ajax,
If there is a get commit, do not want it to request, in Django has the adorner this parameter to set.
CSRF validation of the Django framework in Python