Solution to 403 error in jquery ajax post data in django (two methods), djangojquery

Source: Internet
Author: User

Solution to 403 error in jquery ajax post data in django (two methods), djangojquery

In django, when jquery ajax post data is used, the error 403 is returned.

Method 1:

If jQuery is used to handle ajax, Django directly sends a piece of code to solve the problem. Put it in an independent js file and introduce it to the html page. Note that this js file must be introduced after the jquery js file is introduced.

$(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 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(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'));   } });

Method 2:

Add the @ csrf_exempt modifier before the view for processing the post Data

For example

@csrf_exempt def profile_delte(request):   del_file=request.POST.get("delete_file",'')

The above two methods have been used to solve the django ajax post 403 error. Of course, there are more than these two solutions. You are welcome to share your own opinions. This article is not well written. Please forgive me. Thank you.

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.