This article in two ways to introduce you in Django, using jquery Ajax post data, there will be 403 error, see below for details.
Method One:
If you use jquery to handle Ajax, Django sends a problem-solving code directly. Put it in a separate JS file, in the HTML page can be introduced. Note that this JS file must be in the jquery JS file introduced after the introduction can be
$ (document). Ajaxsend (function (event, XHR, settings) {function GetCookie (name) {var cookievalue = null;
if (document.cookie && document.cookie!= ') {var cookies = Document.cookie.split (';');
for (var i =; i < cookies.length; i++) {var cookie = Jquery.trim (Cookies[i]);
Does this cookie string begin with the name we want? if (cookie.substring (, name.length +) = = (name + ' = ')) {cookievalue = decodeURIComponent (cookie.substring (Name.len
Gth +));
Break
}} return cookievalue; The function sameorigin (URL) {//URL could be relative or scheme relative or absolute var host = document.location. Host
Host + port var protocol = Document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin; Allow absolute or scheme relative URLs to same origin return (URL = origin | | Url.slice (, origin.length +) = = Orig
In + '/') | | (url = = Sr_origin | | url.slice (, sr_oRigin.length +) = = Sr_origin + '/') | |
Or any of the other URLs that isn ' t scheme relative or absolute i.e relative. !
(/^ (\/\/|http:|https:). */.test (URL)); function Safemethod (method) {return (/^ get| head| options|
TRACE) $/.test (method)); } if (!safemethod (settings.type) && Sameorigin (Settings.url)) {Xhr.setrequestheader ("X-csrftoken", GetCookie
(' Csrftoken ')); }
});
Method Two:
Add @csrf_exempt to the view before processing post data
For example
@csrf_exempt
def profile_delte (request):
del_file=request. Post.get ("Delete_file", "")
There are two ways to tell you that Django uses Ajax post data for 403 errors, and I hope it helps.