When Django reaches version 1.2.5 or later, the use of Ajax POST requests has changed. We still remember that the previous version 1.2 and Django joined the csrf mechanism, greatly enhancing the security of network requests, because of this, Django currently protects the data of any post requests on the page against cross-origin attacks. After you upgrade to version 1.2.5 or later, you will find that the Ajax Post Script methods of the previous version 1.2.4 are not available or have errors. The Django official website provides a solution, add a jquery ajaxsend event so that your previous Ajax Post script can be used again normally. SeeCode:
$ (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 = 0; I <cookies. length; I ++) {var cookie = jquery. trim (Cookies [I]); // does this cookie string begin with the name we want? If (cookie. substring (0, name. length + 1) = (name + '=') {cookievalue = decodeuricomponent (cookie. substring (name. length + 1); break ;}}return cookievalue;} function sameorigin (URL) {// URL cocould 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_orig In; // allow absolute or scheme relative URLs to same origin return (url = Origin | URL. slice (0, origin. length + 1) = Origin + '/') | (url = sr_origin | URL. slice (0, sr_origin.length + 1) = sr_origin + '/') | // or any other URL 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 '));}});
Add code
<SCRIPT type = "text/JavaScript"> $ (document ). ajaxsend (function (event, xhr, settings) {function getcookie (name) {var cookievalue = NULL; If (document. cookie & document. cookie! = '') {Var cookies = Document. Cookie. Split (';');... </SCRIPT>