Django Ajax POST request failure Solution

Source: Internet
Author: User

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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.